Positional parameters beyond $9

Robert Hartman rhartman at thestepchild.sgi.com
Wed Apr 17 10:03:02 AEST 1991


In article <1998 at quando.quantum.de> omerzu at quantum.de (Thomas Omerzu) writes:
>
>In article <991 at dri500.dri.nl> slootman at dri.nl (Paul Slootman) writes:
>
>[...]
>>not seen before. If a script has more than 9 positional parameters,
>>there seems to be no way to *directly* access the tenth onwards. Try
>[...]
>>other way. Nothing in TFM states anything about problems with more
>>than 9 parameters (unless I've overlooked it...)
>
>Quote from sh(1):
>
>        Parameter Substitution
>          The character $ is used to introduce substitutable
>          parameters.  There are two types of parameters, positional
>          and keyword.  If parameter is a digit, it is a positional
>.                                         ^^^^^
>          parameter.  Positional parameters may be assigned values by
>          set.  Keyword parameters (also known as variables) may be
>          assigned values by writing:
>
>
>digit != number :-)

There isn't any way to do that directly.  However, I've never
encounterd a case where I wanted to get at $10 without also wanting to
(or being willing to) get at $1-$9.  Here's an example of how I get at
each argument in turn:

while [ $# -gt 0 ] ; do
	case $1 in
	-*) options="$options $1" ;;
	*)  files="$files $1" ;;
	esac
	shift
done

I suppose you could combine this with some sort of test to get at $10:

tmp="$@" #capture args list in temp variable
while [ $# -gt 0 ] ; do
	if [ `expr $# - 10` -eq 0 ] ; then arg10="$1" ; fi
	shift
done
set $tmp #restore original args list

Or you could use your favorite counting look and stick a shift command in
there.  But for handling a variable argument list, I like the first option
best.

-r



More information about the Comp.unix.shell mailing list