Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 773
The Student Avatar asked Jan 20 '11 18:01

The Student


People also ask

How do I echo a number in bash?

Use bash -x scriptname to debug. Just so you know, in bash, you can do echo {1.. 50} . And in $(()) environment, the variables inside are automatically evaluated, which is why you don't need the $ inside it again.

How do you declare multiple variables in shell script?

Using an Array Another way to assign multiple variables using a command's output is to assign the command output fields to an array. In the example, we used the Bash built-in readarray command to read the date command's output. The default delimiter used by the readarray command is a newline character.


1 Answers

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

echo $((x+1))

See §2.6.4

like image 59
SiegeX Avatar answered Oct 14 '22 08:10

SiegeX