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

2020-02-26 07:35发布

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 token `$x+1'

How can I do that?

9条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-26 08:15

No need for expr, POSIX shell allows $(( )) for arithmetic evaluation:

echo $((x+1))

See §2.6.4

查看更多
beautiful°
3楼-- · 2020-02-26 08:16

echo $((x+1)) also same result as echo $(($x+1))

查看更多
劫难
4楼-- · 2020-02-26 08:20

You can also use the bc utility:

$ x=3;
$ echo "$x+5.5" | bc
8.5
查看更多
登录 后发表回答