[comp.unix.wizards] Is this known to unix.wizards?

allbery@ncoast.UUCP (Brandon S. Allbery) (01/01/89)

Every so often there is mention of /bin/sh's braindamaged handling of "$@"
when there are no arguments.  I think I've worked out a way around it that
doesn't require "if" or "case":

				${1+"$@"}

The above construct should NOT be quoted.  And I'm pretty sure it will work
on all versions of the Bourne shell.  It seems to work in some tests I've
done here.  Anyone else want to comment?  I find it hard to believe that I'm
the first one to notice this, but I've never seen any discussion of it
anywhere and it *certainly* isn't in the manual or in any of the shell books
I've read.

(Analysis for non-wizards:  the construct ${variable+string} returns nothing
if the variable is not set and the value of string if the variable is set.
The string can be quoted and can include other variable substitutions.  If
this *is* the first time anyone's ever seen this particular trick, chalk it
up to the Bourne shell having *very* non-intuitive variable manipulations.)

++A somewhat surprised Brandon
-- 
Brandon S. Allbery, comp.sources.misc moderator and one admin of ncoast PA UN*X
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery@hal.cwru.edu
comp.sources.misc is moving off ncoast -- please do NOT send submissions direct
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>.

leo@philmds.UUCP (Leo de Wit) (01/06/89)

In article <13307@ncoast.UUCP> allbery@ncoast.UUCP (Brandon S. Allbery) writes:
|Every so often there is mention of /bin/sh's braindamaged handling of "$@"
|when there are no arguments.  I think I've worked out a way around it that
|doesn't require "if" or "case":
|
|				${1+"$@"}
|
|The above construct should NOT be quoted.  And I'm pretty sure it will work
|on all versions of the Bourne shell.  It seems to work in some tests I've
|done here.  Anyone else want to comment?  I find it hard to believe that I'm
|the first one to notice this, but I've never seen any discussion of it
|anywhere and it *certainly* isn't in the manual or in any of the shell books
|I've read.

In article <15232@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
   [Subject: Re: make bug]
|echo ${1+"$@"} | sed -e "s/ /$suf /g" -e "s/$/$suf/"
   and
|echo ${1+"$@"} | sed -e "s/$oldsuf /$newsuf /g" -e "s/$oldsuf$/$newsuf/"

so at least Chris knew of the construct (or hacked up his scripts
quickly after seeing Brandon's message 8-).

                                              Leo.

ado@elsie.UUCP (Arthur David Olson) (01/07/89)

In article <13307@ncoast.UUCP>, allbery@ncoast.UUCP (Brandon S. Allbery) writes:
> Every so often there is mention of /bin/sh's braindamaged handling of "$@"
> when there are no arguments.  I think I've worked out a way around it that
> doesn't require "if" or "case":
> 				${1+"$@"}
> . . .I find it hard to believe that I'm the first one to notice this,
> but I've never seen any discussion of it anywhere and it *certainly*
> isn't in the manual or in any of the shell books I've read.

I've attached the earliest mention of the idiom that I can find;
I don't recall how I first learned of it, but it's used in a number of scripts
here at elsie now (sccs work and carbon dating show December of '86 as the
earliest occurrence).  Thanks to David Giacomo (or whomever) for the discovery!
--
UNIX is a registered trademark of AT&T.
-- 
	Arthur David Olson    ado@ncifcrf.gov    ADO is a trademark of Ampex.

< Path: elsie!cvl!umcp-cs!seismo!ut-sally!pyramid!decwrl!sun!david
< From: david@sun.uucp (David DiGiacomo)
< Newsgroups: net.unix-wizards
< Subject: Re: Problems with System V Release 2.1 on 68010
< Message-ID: <3514@sun.uucp>
< Date: 16 Apr 86 18:15:14 GMT
< References: <401@oscvax.UUCP> <412@hropus.UUCP>
< Reply-To: david@sun.uUCp (David DiGiacomo)
< Organization: Sun Microsystems, Inc.
< Lines: 18
< 
< In article <412@hropus.UUCP> ka@hropus.UUCP (Kenneth Almquist) writes:
< >> 4) Is it true that the sh construct "$@" (in a shell script) is supposed
< >>    to be identically equal to the command-line arguments? It seems that
< >>    it should, because otherwise there is no way to get at these because
< >>    $@ = $* gets reparsed, and "$*" is one word.
< >>    In our sh, "$@" is exactly equivalent to the command line args if there
< >>    are some, otherwise it is equal to "" instead of nothing.
< >>    Seems like a bug to me. If so, how wide-spread is it?
< >
< >It's on our SVR2 on a VAX here.  The Korn shell gets this right, of course.
< 
< This also afflicts SunOS 3.0.  I find it incredibly annoying, but a simple
< workaround is to use ${1+"$@"} instead of plain "$@".
< 
< [disclaimer]
< -- 
< David DiGiacomo  {decvax, ihnp4, ucbvax}!sun!david  david@sun.arpa
< Sun Microsystems, Mt. View, CA  (415) 960-7495