I have a variable x=7 and I want to repeat it plus one, like echo ($x+1) , but I get:
x=7
echo ($x+1)
bash: syntax error near unexpected token `$ x + 1 '
How can i do this?
There is no need for expr , the POSIX shell allows $(( )) for arithmetic evaluation:
expr
$(( ))
echo $((x+1))
See & sect; 2.6.4
Try double parentheses:
$ x=7; echo $(($x + 1)) 8
You can also use the bc utility:
bc
$ x=3; $ echo "$x+5.5" | bc 8.5
try echo $ (($ x + 1))
I think it only works on some version of bash that is 3 or more.
echo `expr $x + 1`
will be another solution
Just use the expr command:
$ expr $x + 1 8
We use expr for this:
Try as follows:
echo $(( $X + 1 ))
$ echo $(($x+1)) 8
From man bash :
man bash
Arithmetic expansionArithmetic expansion allows you to evaluate the arithmetic expression and the substitution of the result. Format for arithmetic expansion: $((expression)) The expression is treated as if it were in a double quote, but the double quote inside the parentheses is not specially processed. All tokens in the expression undergo parameter expansion, line expansion, command substitution, and removal of quotes. Arithmetic substitutions can be nested.The assessment is carried out in accordance with the indicated rules below under the ARIMETIC EVALUATION. If the expression is invalid, bash prints an error message and no substitution occurs.
Arithmetic expansion
Arithmetic expansion allows you to evaluate the arithmetic expression and the substitution of the result. Format for arithmetic expansion:
$((expression))
The expression is treated as if it were in a double quote, but the double quote inside the parentheses is not specially processed. All tokens in the expression undergo parameter expansion, line expansion, command substitution, and removal of quotes. Arithmetic substitutions can be nested.
The assessment is carried out in accordance with the indicated rules below under the ARIMETIC EVALUATION. If the expression is invalid, bash prints an error message and no substitution occurs.
echo $ ((x + 1)) is also the same result as echo $ (($ x + 1))
Source: https://habr.com/ru/post/651313/More articles:JPanel inside another - javashoot a projectile (direct trajectory) at a moving target in 3 dimensions - algorithmLock free read only List in Python? - performanceWhy is the output of my branching program different when giving output? - cFileUpload with Django - filejava: reflection to get Enum - javaHow to use TagLib to read / write Coverart in different audio formats? - c ++Is Derby / Java DB included in Java 6? - javaAre all equality mappings in PHP symmetric? - comparisonIs the == operator transitive in PHP? - equalityAll Articles