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

回眸只為那壹抹淺笑 提交于 2021-02-07 11:57:15

问题


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, ...?


回答1:


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....?"



来源:https://stackoverflow.com/questions/18318716/why-do-bash-command-line-arguments-after-9-require-curly-brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!