I am using bash shell on linux and want to use more than 10 parameters in shell script
Asked
Active
Viewed 8.9k times
125
-
8Note that having 10 parameters will make it quite confusing. Perhaps it would be better to use options (e.g. `-a 1` or `--foo=bar`) instead. See `man getopt`, `man getopts`, and `man bash` for some options for doing that. – Mikel Feb 06 '11 at 10:34
-
See also: [Why do bash command line arguments after 9 require curly brackets?](https://stackoverflow.com/questions/18318716/why-do-bash-command-line-arguments-after-9-require-curly-brackets) – John Kugelman Apr 03 '20 at 16:37
2 Answers
200
Use curly braces to set them off:
echo "${10}"
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
Dennis Williamson
- 324,833
- 88
- 366
- 429
-
3Note that ${10} will work in bash, but will limit your portability since many implementations of sh only allow single digit specifications. – William Pursell Feb 06 '11 at 14:11
-
1@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. – Dennis Williamson Feb 06 '11 at 15:34
-
2@WilliamPursell `${10}` is defined [by POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02) – Zombo Dec 09 '16 at 19:15
-
2Worrying 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. – William Pursell Dec 10 '16 at 16:54
33
-
5I 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/*` – Dennis Williamson Feb 06 '11 at 10:33
-
3My shell comfortably goes up to 2 million `set $(seq 2097152); echo ${2097152}` – Zombo Dec 09 '16 at 19:32