bash - bash 中 $[a-b] 和 $((a-b)) 之间的区别

标签 bash shell math operator-keyword arithmetic-expressions

我不认识这个运算符 $[],也找不到有关它的信息。但是我知道接下来的两个代码给出相同的输出

a=4
b=1
echo $[a-b] # => 3

a=4
b=1
echo $((a-b)) # => 3

那么 $[] 运算符的作用是什么,与 $(()) 有什么区别?

在我的 zsh shell 提示符中,当我打开其中任何一个并且没有关闭它们时,我已经编写了mathsubst

最佳答案

阅读 man bash 表示旧格式 $[expression] 已弃用并将被删除。否则它们应该是等效的。

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

          $((expression))

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

关于bash - bash 中 $[a-b] 和 $((a-b)) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41081417/

相关文章:

linux - "Read"每行跳过最后一个字

language-agnostic - 为什么我看不到大多数高级语言中的管道运算符?

math - 球面坐标到平面的距离

javascript - 数据系列积分

algorithm - 将给定数字表示为四个平方和

linux - Bash 等待多个同时的子进程并在出错时杀死所有子进程

Bash/sed 字符串替换为文件内容(包含换行符)

bash - awk:如何从文件 A 中提取文件 B 中指定索引的列?

javascript - 为什么 WScript 不起作用?

bash - 无法将变量从 Jenkins 中的执行 shell 导出到其他项目(使用属性文件)