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?
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.
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.
No need for expr
, POSIX shell allows $(( ))
for arithmetic evaluation:
echo $((x+1))
See §2.6.4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With