150

I am using bash shell on linux and want to use more than 10 parameters in shell script

2

2 Answers 2

240

Use curly braces to set them off:

echo "${10}"

Any positional parameter can be saved in a variable to document its use and make later statements more readable:

city_name=${10}

If fewer parameters are passed then the value at the later positions will be unset.

You can also iterate over the positional parameters like this:

for arg

or

for arg in "$@"

or

while (( $# > 0 ))    # or [ $# -gt 0 ]
do
    echo "$1"
    shift
done
4
  • 5
    Note that ${10} will work in bash, but will limit your portability since many implementations of sh only allow single digit specifications. Commented Feb 6, 2011 at 14:11
  • 2
    @William: There are some shells that won't accept it, such as the original legacy Bourne shell, but in addition to the shells I listed in another comment (Bash, dash, ksh and zsh), it also works in csh, tcsh and Busybox ash. Commented Feb 6, 2011 at 15:34
  • 3
    @WilliamPursell ${10} is defined by POSIX
    – Zombo
    Commented Dec 9, 2016 at 19:15
  • 4
    Worrying about ${10} working is only necessary when using very old implementations which are not standard compliant. Probably only of historical interest...and yet I have yet to ever use it! I suppose because best practice dictates that 10 arguments is way too many unless they are repeated, in which case you'll iterate over them with "$@" rather than enumerating them. Commented Dec 10, 2016 at 16:54
39

You can have up to 256 parameters from 0 to 255 with:

${255}
3
  • 8
    I think that limit is dependent on the shell. Bash, dash, ksh and zsh don't seem to have it. sh -c 'echo ${333}' /usr/bin/* Commented Feb 6, 2011 at 10:33
  • 6
    My shell comfortably goes up to 2 million set $(seq 2097152); echo ${2097152}
    – Zombo
    Commented Dec 9, 2016 at 19:32
  • 3
    You can run getconf ARG_MAX to find out the max number of parameters your system can handle.
    – Lorand
    Commented Jan 17, 2023 at 2:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.