$* and $@ (Was: nohup (from Bourne shell))

karl at haddock.UUCP karl at haddock.UUCP
Sat Dec 13 11:36:41 AEST 1986


Newsgroups: comp.bugs.4bsd,comp.unix.questions
Subject: $* and $@ (Was: nohup (from Bourne shell))
References: <181 at haddock.UUCP> <106 at quacky.UUCP>
Reply-To: karl at haddock.ISC.COM.UUCP (Karl Heuer)
Followup-To: comp.unix.questions
Distribution: world
Organization: Interactive Systems, Boston

In article <106 at quacky.UUCP> dce at quacky.UUCP (David Elliott) writes:
>In article <181 at haddock.UUCP> karl at haddock.isc.com.UUCP (Karl Heuer) writes:
>>The shell script /usr/bin/nohup uses `$*' where it should use `$@'; this
>>breaks things like `nohup sh -c "echo foo"' because the quotes get lost.
>
>Yes and no. Nohup should say   exec nice -5 "$@"

That's what I meant, of course.  Thanks for the clarification.

>There is one slight problem with "$@" [namely, when $# = 0 it gives you a
>null string instead of nothing at all].  I've discussed this bug with
>Steve Bourne, and he agrees that it is incorrect behavior.

Good!  I've been trying for years to convince people that it's a bug.

>Add [code elided] to the beginning of the subroutine macro() in macro.c, and
>the bug goes away. It is somewhat kludgy, but it works.

The whole concept of "$@" is somewhat kludgy.  (Why doesn't "a$@b" return
"a$1b" "a$2b" ... ?)  But it's indispensible.

>Also, while I'm here, I'll show a way to properly build a list into a single
>variable. Typically, this is done as
>	for i { List="$List $i"; }
>	command $List
>This is incorrect, since it breaks if an argument contains spaces or tabs
>or newlines or what have you. The following works much better:
>	for arg { Arg_list="$Arg_list '$arg'"; }
>	eval command "$Arg_list"

Better, but now it breaks if an argument contains a single quote.  Is there a
method that preserves all the printing characters, and works on all versions
of sh?  (I tried to do something with   sed 's/[\\"`$]/\\&/'  and stuffing the
result into double quotes, but I couldn't find a portable mechanism.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
(Followups to comp.unix.questions, unless you have more bugs to report.)



More information about the Comp.bugs.4bsd.ucb-fixes mailing list