On 06Apr2016 19:33, derek martin <inva...@pizzashack.org> wrote:
On Thu, Apr 07, 2016 at 09:38:43AM +1000, Cameron Simpson wrote:
> if $1 is set use that, otherwise use "$@" (all arguments, individually quoted)
No, it says ``if $1 is set, use "$@", otherwise use nothing''. See below.
I see. So I had it exactly backwards. :)
I often have to look these up too. I mostly use :- and := myself, aside from
this special incantation.
Firstly, "$@" _is_ portable; it has been around as a very special
case for decades, since at least V7 UNIX.
For sure. It was the ${keword+expression} syntax I was referring to.
I didn't remember coming across this syntax until I started using
HP-UX 10.0 with "the POSIX shell" so I assumed it was not portable,
but it seems that it is.
I've been using this incantation since the mid-80s, on systems older than that,
so the basic kw+expr is plenty old. The newer pattern matching things are much
younger, and IIRC only in bash and zsh and similar, not core Bourne shell.
Then again, maybe I did know that once--my
memory chips have become increasingly faulty with age. ;-)
Me too. I am further hampered by little "calendar" time sense; other people can
say "oh this happened in 2001", I have no idea and have to reference wrt to
specific events in my memory, hoping they have dates associated.
For historic reasons, "$@" evaluates to a single "" if there were no
arguments at all, introducing a spurious new empty argument.
Hmmm... This is news to me, and my quick test does not bear it out:
Probably fixed in modern interactive shells. Note your test is testing your
interactive shell, not /bin/sh (if they're different).
$ cat foo.sh
#!/bin/sh
echo "$# command line args"
$ ./foo.sh "$@"
0 command line args
$ ./foo.sh ""
1 command line args
-=-=-=-
$ cat foo.c
#include <stdio.h>
int main(int argc, char **argv)
{
printf("got %d command line args\n", argc - 1);
return 0;
}
$ ./foo
got 0 command line args
$ ./foo "$@"
got 0 command line args
$ ./foo ""
got 1 command line args
Both script and C program are testing your interactive shell (bash? mine is
usually zsh).
BTW, you might repeat the test with a bare $@ as well. May be informative. I
believe it should work like "$@", versus $* which works like normal variables.
In any event, it appears that your paranoid version at the least does
no harm. :)
True, which is why my fingers continue to know it:-)
Cheers,
Cameron Simpson <c...@zip.com.au>