SUMARY: /bin/sh commandline arguments

From: Gautam Das (gautam@bwc.org)
Date: Tue Jan 14 1997 - 02:53:51 CST


Thanks to all who responded. The answer is - it is limitation of sh and not
a bug, and is documented in the man pages, if you read carefully...:)

There were 2 wrong answers, suggesting I try $10 instead of ${10}.

(Note: If you use $10, you get the value of $1 with 0 appended to it :).. )

Any way the original query is below, followed by the responses.

On Mon, 13 Jan 1997, Gautam Das wrote:

> I cannot reference more than 9 command-line arguments using
> /bin/sh. I get a "bad substitution" error.
>
> However bash runs the same script with no errors.
>
> Here's a few test scripts
>
> 1. test1.sh
> #!/bin/sh
> echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}
>
> # ./test1.sh 1 2 3 4 5 6 7 8 9 10 11
> ./test1.sh: bad substitution
>
> 2. test2.sh that works when referencing upto 9 arguments
> #!/bin/sh
> echo $1 $2 $3 $4 $5 $6 $7 $8 $9
>
> # ./test2.sh 1 2 3 4 5 6 7 8 9
> 1 2 3 4 5 6 7 8 9
>
> And here's test.bash that works for more that 9 arguments
>
> #!/usr/local/bin/bash
> echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}
>
> # ./test.bash 1 2 3 4 5 6 7 8 9 10 11
> 1 2 3 4 5 6 7 8 9 10 11
>
> Is this correct behaviour of /bin/sh?
>
> I can work around it by reading in $* or by reading in an argument at a
> time and shifting. I am not looking for work around suggestions. I want
> to know if this is a bug?
>
> Thanks.
>
> Gautam

>From fpardo@tisny.com Tue Jan 14 10:33:51 1997
Date: Mon, 13 Jan 1997 11:01:11 -0500 (EST)
From: Frank Pardo <fpardo@tisny.com>
To: gautam@bwc.org
Subject: Re: /bin/sh commandline arguments

> I cannot reference more than 9 command-line arguments using
> /bin/sh. I get a "bad substitution" error.
>
> However bash runs the same script with no errors.
>
> Is this correct behaviour of /bin/sh?

Yes. You can't have more than 9 arguments with /bin/sh, because the
number must be a single digit, not a string of digits within "{}".

> I can work around it by reading in $* or by reading in an argument at a
> time and shifting. I am not looking for work around suggestions. I want
> to know if this is a bug?

No, it is not a bug. The Bourne Shell has always worked this way. The
ability to have more than 9 arguments without using "shift" is one of
the many improvements that came with "bash".

--
  Frank Pardo  <fpardo@tisny.com>
  Transaction Information Systems
  New York City

The scholar's ink outlasts the martyr's blood. -- Irish proverb

>From dwillard@scires.com Tue Jan 14 10:34:04 1997 Date: Mon, 13 Jan 1997 10:59:28 -0500 From: David Willard <dwillard@scires.com> To: gautam@bwc.org Subject: /bin/sh commandline arguments -Reply

It is correct behavior for /bin/sh. /bin/sh allows only 1 digit after the $. It is NOT a 'bug', just a limitation. Apparently bash removes this limitation, I've never used bash.

---------------------------- David Willard Scientific Research Corp. dwillard@scires.com

>From reynolds@acetsw.amat.com Tue Jan 14 10:34:15 1997 Date: Mon, 13 Jan 97 07:59:12 PST From: John Reynolds <reynolds@acetsw.amat.com> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

Hi.

I recall from a shell script course that Bourne shell can't accept more than nine arguments; $10 doesn't translate. That's built into sh, and is one of the things that bash is supposed to fix for you.

Sorry.

John Reynolds No cat anywhere ever gave anyone Applied Materials a straight answer. 2901 Patrick Henry Dr. MS 5502 Santa Clara CA 95054 -Peter Beagle (408) 235-6352 reynolds@acetsw.amat.com

>From cadams@crrel41.crrel.usace.army.mil Tue Jan 14 10:34:26 1997 Date: Mon, 13 Jan 1997 11:05:59 -0500 From: Chad Adams <cadams@crrel41.crrel.usace.army.mil> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

That is the proper behavior os SH. The only wayaround it are the methods you suggested. :)

Chad Adams USACRREL System Administrator (603) 646-4320

>From danno@aa.fv.com Tue Jan 14 10:34:36 1997 Date: Mon, 13 Jan 1997 11:15:27 -0500 (EST) From: Dan Pritts <danno@aa.fv.com> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

On Mon, 13 Jan 1997, Gautam Das wrote: > I cannot reference more than 9 command-line arguments using > /bin/sh. I get a "bad substitution" error.

> Is this correct behaviour of /bin/sh?

yes, this is the way it acts. I agree this is suboptimal.

"who would ever need to read more than 9 command line arguments anyway?"

:)

danno

>From johnb@soliton.com Tue Jan 14 10:34:49 1997 Date: Mon, 13 Jan 1997 11:30:30 -0500 From: john benjamins <johnb@soliton.com> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

yup, the "standard" bourne shell handles $1, ..., $9 only. your workaround is the only way around it (or going to ksh). it's not a bug, it's a limitation.

hope this helps, -john

---- john benjamins johnb@soliton.com "scientists are explorers, philosophers are tourists." - r. p. feynman

>From kpischke@cadence.com Tue Jan 14 10:34:58 1997 Date: Mon, 13 Jan 1997 17:28:42 +0100 From: Karlheinz Pischke <kpischke@cadence.com> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

This is the same in all shells that you can only address $1 - $9 you can use shift to move to the next parameter.

#!/bin/sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 x1=$1 .. x8=$8 shift 8 echo $1 $2 $3 $4 $5 $6 $7 $8 $9 # $1 should now be $9 from above ..

look also for getoptcvt in man pages

Mit freundlichen Gruessen / Best Regards

Karlheinz Pischke

Karlheinz Pischke, Dipl.-Inf., Applikationsingenieur

Email: kpischke@cadence.com C A D E N C E GmbH Richard-Reitzner-Allee 8 (H 2) Tel.: 49 - 89 - 4563-0 85551 Haar Fax: 49 - 89 - 4563-1919 Germany

>From rlb@cruiser.engin.umb.edu Tue Jan 14 10:35:05 1997 Date: Mon, 13 Jan 1997 11:39:25 -0500 (EST) From: "Robert L. Bailey" <rlb@cruiser.engin.umb.edu> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

You have to use shift to get beyond the 9th argument. Here's an example.

file: foo

#!/bin/sh echo $* echo $1 $2 $3 $4 $5 $6 $7 $8 $9 shift echo $1 $2 $3 $4 $5 $6 $7 $8 $9 _EOF_

./foo 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 10

>From nadya@bog.ucsd.edu Tue Jan 14 10:35:15 1997 Date: Mon, 13 Jan 97 09:00:37 PST From: Nadya Williams <nadya@bog.ucsd.edu> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

If you use just the

#!/bin/sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11

e.a. without the "{" and "}" you woudl be able to reference all the arguments you want.

Nadya Williams, UCSD

>From mjb@ltx.com Tue Jan 14 10:35:22 1997 Date: Mon, 13 Jan 1997 11:42:54 -0500 From: Mark Belanger <mjb@ltx.com> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

Hi,

I tried the same thing with the Korn shell. If you remove the brackets arount $10 and $11 it works. I got the same error with the brackets in place:

Here are my results: oasis:mjb cat test1.sh #!/bin/sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}

oasis:mjb test1.sh test1.sh: bad substitution

Now I fix test1.sh and rerun it.

oasis:mjb cat test1.sh #!/bin/sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 oasis:mjb test1.sh 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11

-Mark

Mark Belanger Software Technician * Email : mjb@ltx.com * LTX Corporation * Voice : (617) 467-5021 * LTX Park at University Ave * Fax : (617) 329-6880 * Westwood, MA 02090-2306, USA

>From rtrzaska@uk.mdis.com Tue Jan 14 10:35:27 1997 Date: Mon, 13 Jan 1997 17:02:08 GMT From: Ray Trzaska <rtrzaska@uk.mdis.com> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

Gautam, in sh positional parameters are a single digit, in korn shell they are as bash, and if more than a single digit must be enclosed in braces.

so your example must eith be run under the korn shell ( /bin/ksh ) or do something like ...

1. test1.sh #!/bin/sh p1="$1" p2="$2" shift 2 echo $p1 $p2 $1 $2 $3 $4 $5 $6 $7 $8 $9

2. test1.ksh should give the same result #!/bin/ksh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}

ray.

>From mrs@cadem.mc.xerox.com Tue Jan 14 10:35:36 1997 Date: Mon, 13 Jan 1997 09:13:51 PST From: Michael Salehi x22725 <mrs@cadem.mc.xerox.com> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

Gautam,

That's sh limitation.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Mike (Mehran) Salehi mrs@cadem.mc.xerox.com (716) 422-2725 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

>From brian.styles@mrc-bsu.cam.ac.uk Tue Jan 14 10:35:47 1997 Date: Mon, 13 Jan 1997 17:14:36 GMT From: Brian Styles <brian.styles@mrc-bsu.cam.ac.uk> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

Dear Gautam,

Yes, I believe this behaviour is in accord with the man pages, viz:

for sh: -------

Parameter Substitution The character $ is used to introduce substitutable parame- ters. There are two types of parameters, positional and keyword. If parameter is a digit, it is a positional param- eter.

for bash: ---------

Positional Parameters A positional parameter is a parameter denoted by one or more digits, other than the single digit 0.

I guess that your workarounds are probably the practical answer. In any case, it's a bit inelegant to be handing >9 arguments to any script...

-Brian

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_* Dr Brian C. Styles, Computing Systems Manager Medical Research Council Telephone: 01223 330380 Biostatistics Unit International: +44 1223 330380 IPH, University Forvie Site Fax: +44 1223 330383 Robinson Way Cambridge CB2 2SR. Internet: brian.styles@mrc-bsu.cam.ac.uk ENGLAND. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

>From ric@rtd.com Tue Jan 14 10:35:52 1997 Date: Mon, 13 Jan 1997 10:25:30 -0700 (MST) From: Ric Anderson <ric@rtd.com> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

Historical restriction, stupid bug, or feature. Regardless of how you look at it, a genuine Bourne shell only allows $n to be 1-9, The Korn shell (ksh) allows n bigger than 9, but only if referenced as ${n}, etc.

You didn't specify if you were running SunOS 4 or Solaris 2.x. If the latter, I'd suggest using /bin/ksh for scripts, as it is upward compatible with sh for all the stuff I've seen.

Cheers, Ric (<ric@rtd.com> "Ric Anderson", using RTD's public internet access)

>From Matthew.Stier@mci.com Tue Jan 14 10:35:58 1997 Date: Mon, 13 Jan 1997 12:33:21 -0500 From: Matthew Stier <Matthew.Stier@mci.com> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

Read the manpage. Its in there.

(Yes, it is the correct behavior)

-- Matthew Stier matthew.stier@mci.com

----------

>From raju@ecologic.net Tue Jan 14 10:36:07 1997 Date: Mon, 13 Jan 1997 13:02:05 -0500 (EST) From: raju@ecologic.net To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

hoho% cat test1.sh #!/bin/sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 hoho% ./test1.sh 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 hoho%

it seems $10 is correct but ${10} give you the bad substitution error

--raju

Raju Krishnamurthy - Senior Software Engineer ____________________________________________________

E C O l o g i c C o r p o r a t i o n http://www.ecologic.net

raju@ecologic.net 19 Eye Street, N.W. Washington, DC 20001

v. 202.218.4100 f. 202.842.5088

>From Kevin.Sheehan@uniq.com.au Tue Jan 14 10:36:24 1997 Date: Tue, 14 Jan 1997 07:14:49 EST From: Kevin Sheehan {Consulting Poster Child} <Kevin.Sheehan@uniq.com.au> To: Gautam Das <gautam@bwc.org> Subject: Re: /bin/sh commandline arguments

I'd say it is not desirable behavior, but given:

keyword. If parameter is a digit, it is a positional param-

from sh(1), I'd say it is *defined* as a single digit, so not a bug.

l & h, kev

>From Glenn.Satchell@uniq.com.au Tue Jan 14 10:36:32 1997 Date: Tue, 14 Jan 1997 11:29:39 +1100 (EST) From: Glenn Satchell - Uniq Professional Services <Glenn.Satchell@uniq.com.au> To: gautam@bwc.org Subject: Re: /bin/sh commandline arguments

I don't think it's a bug, it's just how it works.

regards, -- Glenn Satchell glenn@uniq.com.au | There's a fine line Uniq Professional Services Pty Ltd ACN 056 279 335 | between fishing and PO Box 70, Paddington, NSW 2021, (Sydney) Australia | standing on the shore Phone 02 380 6360 Pager 016 287 000 Fax 02 380 6416 | looking like an idiot. VISIT OUR NEW WEB SITE http://www.uniq.com.au



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:42 CDT