The use of ${1+"$@"} is a bit of a cargo cult thing these days, but I can remember the reason for it, and it's not unthinkable that it is still needed on the kinds of platforms where the rest of the gubbins are necessary.

Early versions of /bin/sh (this is not going back to V7; it was present in 4BSD and probably into the 1990s on many flavors of Unix - but not Linux) did not do the right thing when there were no arguments to be quoted. If you used "$@" when there were no arguments, this was replaced by an empty string - rather than by nothing at all. Needless to say, an empty string argument is rather different from no argument, compare e.g. cat and cat '' for a simple example.

The ${1+"$@"} is used to expand to exactly nothing if the first argument is not set (the ${1+ } part does this), but if the first argument is set (i.e. there are some arguments) it expands into "$@" which expands into each of the arguments, quoted against being broken at whitespace.

If you didn't care about the whitespace, you see, you could just as easily use $* with no quotes (and a lot of Unix folks who would sooner dine with the devil than put a space in a filename still write shell scripts that way). But if you remember the early early early days of Perl, when Larry Wall was better known for a shell script generating tool called Configure, you can imagine why this ${1+"$@"} is still used.

Nowadays, of course, ksh and bash and pretty much any modern shell that claims POSIX 'sh' compatibility does the right thing with just "$@" and you can use that in your scripts in most situations.


In reply to Re^4: Running Under Some Shell by dupuy
in thread Running Under Some Shell by Xiong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.