bash: $[<arithmetic-expression>] 与 $((<arithmetic-expression>))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2415724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:46:30  来源:igfitidea点击:

bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))

bashsyntaxoperatorslegacyarithmetic-expressions

提问by Chen Levy

I have just stumbled upon the bash syntax:

我刚刚偶然发现了 bash 语法:

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:

当我用谷歌搜索时,我发现 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...

3.4.6. 算术展开

算术扩展允许计算算术表达式并替换结果。算术展开的格式为:

$(( EXPRESSION )) 

...

在可能的情况下,Bash 用户应该尝试使用带方括号的语法:

$[ EXPRESSION ] 

但是,这只会计算 EXPRESSION 的结果,而不会进行测试...

In my bash man page I can only find the $(( EXPRESSION ))form such as:

在我的 bash 手册页中,我只能找到如下$(( EXPRESSION ))形式:

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 $((...))?

那么,哪些测试不使用$[...]$((...)), 或者$[...]只是旧版本$((...))

回答by sth

The manpage for bash v3.2.48 says:

bash v3.2.48 的联机帮助页说:

[...] The format for arithmetic expansion is:

     $((expression))

The old format $[expression] is deprecatedand will be removed in upcoming versions of bash.

[...] 算术扩展的格式是:

     $((expression))

老格式$ [表达]已被弃用,并且将在bash的即将到来的版本中删除。

So $[...]is old syntax that should not be used anymore.

所以$[...]是旧语法,不应该再使用。

回答by Brandon Rhodes

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

@sth 是完全正确的。如果您对为什么现在支持更详细的语法感到好奇,请查看邮件列表中的这封旧电子邮件。

http://lists.gnu.org/archive/html/bug-bash/2012-04/msg00033.html

http://lists.gnu.org/archive/html/bug-bash/2012-04/msg00033.html

“In early proposals, a form $[expression] was used. It was functionally equivalent to the "$(())" of the current text, but objections were lodged that the 1988 KornShell had already implemented "$(())" and there was no compelling reason to invent yet another syntax. Furthermore, the "$[]" syntax had a minor incompatibility involving the patterns in case statements.”

“在早期的提案中,使用了 $[expression] 形式。它在功能上等同于当前文本的“$(())”,但有人提出反对意见,即 1988 年的 KornShell 已经实现了“$(())”,并且没有令人信服的理由去发明另一种语法。此外,“$[]”语法与 case 语句中的模式存在轻微的不兼容。”

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?

我不确定我是否喜欢“但有人已经更详细地完成了这件事”的理由,但是你明白了——也许案例陈述问题比我从这个晦涩的提及中想象的更引人注目?