How do I echo a sum of a variable and a number?

前端 未结 9 1017
眼角桃花
眼角桃花 2020-12-30 05:03

I have a variable x=7 and I want to echo it plus one, like echo ($x+1) but I\'m getting:

bash: syntax error near unexpected

9条回答
  •  滥情空心
    2020-12-30 05:44

    $ echo $(($x+1))
    8
    

    From man bash:

    Arithmetic Expansion

    Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

        $((expression))
    

    The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, string expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.

    The evaluation is performed according to the rules listed below under ARITHMETIC EVALUATION. If expression is invalid, bash prints a message indicating failure and no substitution occurs.

提交回复
热议问题