Bash: let statement vs assignment

前端 未结 1 457
广开言路
广开言路 2020-12-23 16:22

What is the difference between assigning to a variable like var=foo and using let like let var=foo? Or cases like var=${var}bar and

相关标签:
1条回答
  • 2020-12-23 16:50

    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
    
    0 讨论(0)
提交回复
热议问题