65

var=foo分配给变量 like和使用 let like有什么区别let var=foo?或者像var=${var}barand之类的情况let var+=bar?每种方法的优点和缺点是什么?

4

1 回答 1

76

let does exactly what (( )) do, it is for arithmetic expressions. There is almost no difference between let and (( )).

Your examples are invalid. var=${var}bar is going to add word bar to the var variable (which is a string operation), let var+=bar is not going to work, because it is not an arithmetic expression:

$ var='5'; let var+=bar; echo "$var"
5

Actually, it IS an arithmetic expression, if only variable bar was set, otherwise bar is treated as zero.

$ var='5'; bar=2; let var+=bar; echo "$var"
7
于 2013-09-09T18:51:46.697 回答