Shell Script – Accessing the 14th Positional Parameter Using $14

shell-script

I was reading about positional parameters in Unix and then I found this info:

The shell allows a command line to contain at least 128 arguments;
however, a shell program is restricted to referencing only nine
positional parameters, $1 through $9, at a given time. You can work
around this restriction by using the shift command.

So I created a simple shell script called file like the following:

#! /bin/bash
echo $14

then ran it like the following :

./file 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

and I got 14!

So how is that possible if the shell doesn't allow more than 10 parameters (from $0 to $9) without using shift command?

Best Answer

When you run

echo $14

what happens is that bash interprets the argument $14 as $1 and 4 separately. It then expands $1 (which in this case is equal to "1"), then appends the string 4 to it, which results in "14". Although that was the result you were expecting, it's actually a side effect from Bash's actual behaviour. Like @steeldriver mentioned in comments, running your script like this instead :

./file a b c d e f g h i j k l m n

and then calling echo $14 won't output "n" but "a4".

Note that wrapping the variable in double-quotes :

echo "$14"

will still not expand the variable correctly in Bash. The standard method is to use curly braces around the variable name :

echo ${14}

For more information, see the official documentation for parameter expansion in Bash. It can do a lot more cool things too, like

${14^^*}

to capitalize argument no.14. Give it a read! :)

Related Question