Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do bash command line arguments after 9 require curly brackets?

This may not be the most thought provoking question, but nevertheless has struck my curiosity. I have not been able to come across any answer (let alone a definitive one) on the web.

While reading Advanced Shell Scripting, I came across this section regarding command line positional arguments which states that anything after the the ninth argument must be surrounded by ${} (the longer form of variable referencing/substitution).

Simply put, why must you reference command line argument ten (and beyond) as ${10}, ${11}... instead of $10, $11, ...?

like image 890
Default Avatar asked Aug 19 '13 16:08

Default


1 Answers

Specifically, your question relates to "positional parameters." Using $var instead of ${var} is shorthand in bash. In most cases it works well. Bash variables must start with a letter or underscore. It internally treats variables that start with a digit as a "positional parameter." When bash detects a positional parameter it only looks at the first digit, which is why $10 returns $1"0". By calling ${10} you are instructing bash to look at the complete variable instead of its built-in default of the first digit.

As to why it is this way? I have no idea. Legacy implementation which has been expanded upon is my guess. "Who would ever need more than....?"

like image 154
dtorgo Avatar answered Oct 18 '22 20:10

dtorgo