Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle more than 10 parameters in shell

People also ask

How do I pass more than 9 arguments in shell script?

If there are more than 9 arguments, then tenth or onwards arguments can't be assigned as $10 or $11. You have to either process or save the $1 parameter, then with the help of shift command drop parameter 1 and move all other arguments down by one. It will make $10 as $9, $9 as $8 and so on.

What does $# mean in shell?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

Why are curly braces needed for multi digit positional parameters?

The reason the braces are needed to access elements beyond element 9 in most POSIX-like shells is because the POSIX standard says so. A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.

What does $() mean in shell?

$(...) allows command substitution, i.e. allows the output of a command to replace the command itself and can be nested.


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

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

${255}