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

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

问题描述

这可能不是最发人深省的问题,但仍然引起了我的好奇.我无法在网络上找到任何答案(更不用说确定的答案了).

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.

在阅读 Advanced Shell Scripting 时,我遇到了本节关于命令行位置参数声明第九个参数之后的任何内容都必须用 ${}(变量引用/替换的较长形式)包围.

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(及以上)引用为 ${10}, ${11}... 而不是 $10, $11, ...?

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

推荐答案

具体来说,您的问题与位置参数"有关.使用 $var 而不是 ${var} 是 bash 中的简写.在大多数情况下,它运行良好.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....?"

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

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