Trying to figure out how to convert an argument to an integer to perform arithmetic on, and then print it out, say for addOne.sh
:
echo $1 + 1
>>sh addOne.sh 1
prints 1 + 1
Trying to figure out how to convert an argument to an integer to perform arithmetic on, and then print it out, say for
|
||||
In bash, one does not "convert an argument to an integer to perform arithmetic". In bash, variables are treated as integer or string depending on context. To perform arithmetic, you should invoke the arithmetic operator
You should be aware that the shell only performs integer arithmetic. If you have floating point numbers (numbers with decimals), then there are other tools to assist. For example, use
|
|||
|
In
Floating number will be converted to integer, while anything are not look like a number will be converted to 0. Exponentiation will be truncated to the number before Example:
|
||||
|
printf "1 + %s\n" $1 won't do ?
– Archemar Sep 27 '15 at 19:50