Shell script with 10+ parameters? Remember this….

Hi,

This post is mainly aimed towards shell script newbies like myself and the goal is that they don’t end up wasting time on this as I had to.

So, if you are creating a new shell script that requires 10 or more parameters, you need to remember one thing that you can’t access the 10th and further parameters by just $10 or $11, etc.

If you simply put something like below,

...
TenthParameter=$10
EleventhParam=$11
echo $TenthParameter
echo $EleventhParameter
...

The output would actually look like below: (Assume First’s parameter’s value is “First”)

First0
First1

and NOT the actual values of the tenth and eleventh parameters.

This is because the bash interpreter first sees $1 (in the “$10”) and replaces its value immediately.

To get the value of parameters from and beyond 10th param, you need to put them in Curly {} braces as shown below

...    // # WORKS!
...
TenthParameter=${10}
EleventhParameter=${11}
echo $TenthParameter
echo $EleventhParameter
...

This would print the actual values of the parameters.

Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *