庆典:$ [<算术-EX pression]的计算值与$((小于算术-EX pression>)) [英] bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))

查看:184
本文介绍了庆典:$ [<算术-EX pression]的计算值与$((小于算术-EX pression>))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才在bash的语法跌跌撞撞:

I have just stumbled upon the bash syntax:

foo=42
bar=$[foo+1] # evaluates an arithmetic expression

当我Google对于这一点,我发现<一个href=\"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:

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。算术扩展

3.4.6. Arithmetic expansion

算术扩展允许一个算术前pression的评估和结果的替换。对于算术扩展的格式是:

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$(( EXPRESSION )) 


  
  

...

...

只要有可能,bash用户应尽量选用用方括号的语法:

Wherever possible, Bash users should try to use the syntax with square brackets:

$[ EXPRESSION ] 


  
  

然而,这只会计算EX preSSION的结果,并没有做测试...

However, this will only calculate the result of EXPRESSION, and do no tests...

在我的bash的手册页,我只能找到 $((EX preSSION))的形式,如:

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

foo=42
bar=$((foo+1)) # evaluates an arithmetic expression

那么,什么测试不与 $进行[...] $做((...)),或者是 $ [...] 只是 $的旧版本((...))

推荐答案

对于bash v3.2.48的手册页说:

The manpage for bash v3.2.48 says:

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

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

     $((expression))


  
  

旧格式$ [EX pression]是德precated ,将在即将到来的版本中删除
  庆典的

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

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

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

这篇关于庆典:$ [&LT;算术-EX pression]的计算值与$((小于算术-EX pression&GT;))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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