How to add values in a variable in Unix shell scripting?

前端 未结 9 1966
谎友^
谎友^ 2021-02-19 02:14

I have two variables called count1 and count7

count7=0
count7=$(($count7 + $count1))

This shows an error \"expression is not complete; more tok

9条回答
  •  爱一瞬间的悲伤
    2021-02-19 02:43

    What is count1 set to? If it is not set, it looks like the empty string - and that would lead to an invalid expression. Which shell are you using?

    In Bash 3.x on MacOS X 10.7.1:

    $ count7=0
    $ count7=$(($count7 + $count1))
    -sh: 0 + : syntax error: operand expected (error token is " ")
    $ count1=2
    $ count7=$(($count7 + $count1))
    $ echo $count7
    2
    $
    

    You could also use ${count1:-0} to add 0 if $count1 is unset.

提交回复
热议问题