一:基本语法

  • $((运算式))
  • $[运算式]
  • expr 运算式

二:案例操作

1
2
3
4
5
6
7
8
9
[root@hadoop100 shell]# A=$[2+3]
[root@hadoop100 shell]# echo $A
5
[root@hadoop100 shell]# A=$((3+3))
[root@hadoop100 shell]# echo $A
6
# 加号两侧要有空格
[root@hadoop100 shell]# expr 1 + 2
3

三:经验总结

  • expr 中使用 * 时,要转义;例如:a=$(expr 1 * 6)
  • 使用$()或者反引号``,可以执行表达式获取结果;