bash:$[<算术表达式>] 与 $((<算术表达式>))算术表达式>算术表达式>
我刚刚偶然发现了 bash 语法:
foo=42
bar=$[foo+1] # evaluates an arithmetic expression
当我用 Google 搜索时,我发现 http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05:
3.4.6。算术展开
算术展开允许计算算术表达式并替换结果。算术展开式的格式为:
<前><代码>$(( 表达式 ))...
只要有可能,Bash 用户应该尝试使用带方括号的语法:
<前><代码>$[ 表达式 ]但是,这只会计算 EXPRESSION 的结果,并不会进行任何测试...
在我的 bash 手册页中,我可以只找到 $(( EXPRESSION ))
形式,例如:
foo=42
bar=$((foo+1)) # evaluates an arithmetic expression
那么,使用 $[...]
不执行哪些测试,而使用 $((. ..))
,或者 $[...]
只是 $((...))
的旧版本?
I have just stumbled upon the bash syntax:
foo=42
bar=$[foo+1] # evaluates an arithmetic expression
When I Googled for this, I found http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05:
3.4.6. Arithmetic expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$(( EXPRESSION ))
...
Wherever possible, Bash users should try to use the syntax with square brackets:
$[ EXPRESSION ]
However, this will only calculate the result of EXPRESSION, and do no tests...
In my bash man page I can only find the $(( EXPRESSION ))
form such as:
foo=42
bar=$((foo+1)) # evaluates an arithmetic expression
So what tests are not performed with $[...]
that do with $((...))
, or is the $[...]
just a legacy version of $((...))
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bash v3.2.48 的联机帮助页显示:
因此
$[...]
是旧语法,不应再使用。The manpage for bash v3.2.48 says:
So
$[...]
is old syntax that should not be used anymore.@sth是完全正确的。如果您好奇为什么现在更青睐更详细的语法,请查看邮件列表中的这封旧电子邮件。
http://lists.gnu.org/archive/ html/bug-bash/2012-04/msg00033.html
我不确定我是否喜欢这个理由“但有人已经更详细地做到了这一点”,但是你已经明白了——也许案例陈述问题比我从这个模糊的提及中想象的更引人注目?
@sth is entirely correct. And in case you are curious about why a more verbose syntax is now in favor, check out this old email from the mailing list.
http://lists.gnu.org/archive/html/bug-bash/2012-04/msg00033.html
I am not sure that I like the rationale “but someone has already done this more verbosely,” but there you have it—maybe the case-statement problem was more compelling than I am imagining from this obscure mention?