[comp.bugs.4bsd] Arguments to 4.3BSD "sh -c" commands are mishandled

ado@elsie.UUCP (Arthur David Olson) (12/12/86)

Index:	bin/sh 4.3BSD

Description:
	Arguments to a "sh -c" command are mishandled.

Repeat-By:
	Type in the command
		sh -c 'echo $*' a b c d
	and note the incorrect output:
		b c d
--
Sh is a trademark of the American Librarians Association.
--
	UUCP: ..decvax!seismo!elsie!ado   ARPA: elsie!ado@seismo.ARPA
	DEC, VAX, Elsie & Ado are Digital, Borden & Ampex trademarks.

gww@beatnix..UUCP (Gary Winiger) (12/13/86)

It is also appears to be mishandled by /bin/sh on System V Rel 2.2.
When I try ``sh -c 'echo $*' a b c d'' on System V, it also produces
``b c d''.

So 4.3 is probably not the original culprit.  V 2.2 sh has been reworked
to look like C rather than Steve Bourne's original pseudo Algol.

Gary..

kab@reed.UUCP (Kent Black) (12/16/86)

In article <7296@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
 = Index:	bin/sh 4.3BSD
 = 
 = Description:
 = 	Arguments to a "sh -c" command are mishandled.
 = 
 = Repeat-By:
 = 	Type in the command
 = 		sh -c 'echo $*' a b c d
 = 	and note the incorrect output:
 = 		b c d

This is not a bug; $* expands to $1 - $n, i.e.,
skips $0, which happens to be 'a'. 
Try
	sh -c 'echo $0' a b c

-- kab

karl@haddock.UUCP (Karl Heuer) (12/16/86)

[Braces appearing below are used for quoting.  --kwzh]
In article <7296@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes:
>Type in the command { sh -c 'echo $*' a b c d } and note the incorrect
>output: { b c d }.

"That's not a bug, it's a feature!" :-)  "a" is being assigned to $0.  If you
don't care about $0, use a placeholder: { sh -c 'echo $*' sh a b c d }*.

It would be nice if this were documented, though.  I've never seen a man page
for sh that mentions the possibility of giving arguments to a { sh -c }, and
it's been implemented slightly differently in various incarnations.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
*Do not use "-" as the placeholder; argv[0][0]=='-' is magic!