为什么bash命令行参数9之后需要大括号? [英] Why do bash command line arguments after 9 require curly brackets?

查看:51
本文介绍了为什么bash命令行参数9之后需要大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是最令人发人深省的问题,但仍然引起了我的好奇心.我还没有在网络上遇到任何答案(更不用说一个确定的答案)了.

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.

在阅读高级Shell脚本时,我遇到了本节有关命令行位置的信息指出第九个参数之后的所有内容都必须由$ {}包围(变量引用/替换的较长形式).

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).

简而言之,为什么必须将命令行参数十(及以后)引用为 $ {10},$ {11} ... 而不是 $ 10,$ 11 ...?

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

推荐答案

具体来说,您的问题与位置参数"有关.在bash中使用$ var代替$ {var}是简写.在大多数情况下,它运行良好.Bash变量必须以字母或下划线开头.它在内部将以数字开头的变量视为位置参数".当bash检测到位置参数时,它仅查看第一个数字,这就是$ 10返回$ 1"0"的原因.通过调用$ {10},您将指示bash查看完整变量,而不是其内置的第一位默认值.

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

这篇关于为什么bash命令行参数9之后需要大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆