The actual scenarios of the shell involving numerical computations feel relatively few, and more of the scenarios are relational operations. Bash also does not provide numerical computing capabilities, which need to be implemented through expr. Here is a brief record of the common digital calculation methods of expr, and the rest of the pages record the relational operations in detail.

digital computing

expr is an expression evaluation tool used to evaluate expressions.

 a=1
b=2
# ``操作符用于运行内部的表达式并返回执行结果
sum=`expr ${a} + ${b}`

# $()操作符与``功能一样。实际场景中推荐只使用其中一种。
diff=$(expr ${b} - ${a})

# mac中 expr 可以使用 $(()) 代替
diff=$((${b} - ${a}))

Common operators of expr: addition (+), subtraction (-), multiplication (*), division (/), remainder (%). Among them * is a reserved word, which needs to be translated when using: expr 2 /* 2

relational operations

One of the more used scenarios in the shell is the if-else judgment. The shell provides several forms

if test
if []
if [[]]


梦梦她爹
1.8k 声望122 粉丝

引用和评论

0 条评论