in reply to what's the difference between "$@" and ${1+"$@"}

There was a bug in the One True Shell (/bin/sh on V7) such that a simple "$@" would fail miserably if nothing at all was passed. So we old-timers learned to write the extended version instead. Since the code that generates that #! line was no doubt written in the Perl1 days, the legacy was carried forward, although no modern shell probably still suffers the same bug.
  • Comment on Re: what's the difference between "$@" and ${1+"$@"}

Replies are listed 'Best First'.
Re^2: what's the difference between "$@" and ${1+"$@"} (misery)
by tye (Sage) on Oct 06, 2007 at 01:49 UTC
    such that a simple "$@" would fail miserably if nothing at all was passed

    "fail miserably" meant (on my systems back then, anyway) that "" (a single empty argument) got passed instead of no arguments. It was considered more of a design quirk than a bug in my neighborhood, since it made sense for $@ to expand to something like foo" "bar (if two arguments were passed) but didn't make as much sense for $@ to make surrounding quotes, if present, just magically disappear (if no arguments passed).

    - tye        

Re^2: what's the difference between "$@" and ${1+"$@"}
by cadphile (Beadle) on Oct 09, 2007 at 16:56 UTC
    The syntax isn't quite ${1:+$foo}, and hence "this" or "that". I've never seen any documentation of the syntax ${foo+"$bar"}.

    I'm just curious what the 1+ does in this context. If there are actual arguments in "$@", the 1+ doesn't seem to modify the end result at all. So, if "$@" is empty, do we get instead the return value of the first positional parameter $1, which happens also to be empty? Or does the 1+ just act to clean up the return value of the empty "$@".

      From the manual,

      If the colon (:) is omitted from the above expressions, the shell only checks whether parameter is set or not.

        Aha! Now it makes sense. Thanks everyone, especially Merlin, for your imparted wisdom...

        And I finally found a web page that discusses this, with some good background detail.

        http://www.in-ulm.de/~mascheck/various/bourne_args

        As an interesting side note, I wonder if anyone knows how to give this string (${1+"$@"}) to Google's search engine. As is, it doesn't work. I tried single-quoting it, and it still doesn't work.

        Cadphile...