[shell] How to add values
Solved
Hello
I'm discovering the shell in Unix (well, I'm rediscovering it, but at a level much higher than my previous knowledge)
I'm trying to add 2 "integer" values in a script. But since variables in shell are strings, I've been searching and can't find a clean solution.
nbComErr= $txtNbComErr1 + $txtNbComErr2 # => doesn't work.
nbComErr= $(expr $txtNbComErr1 + $txtNbComErr2) # => doesn't work.
echo "($txtNbComErr1 + $txtNbComErr2)" | bc # works fine, but how can I store the result in a variable
(I found a solution using a file, but I don't find that clean...)
If anyone knows, I'm open to solutions :)
I'm discovering the shell in Unix (well, I'm rediscovering it, but at a level much higher than my previous knowledge)
I'm trying to add 2 "integer" values in a script. But since variables in shell are strings, I've been searching and can't find a clean solution.
nbComErr= $txtNbComErr1 + $txtNbComErr2 # => doesn't work.
nbComErr= $(expr $txtNbComErr1 + $txtNbComErr2) # => doesn't work.
echo "($txtNbComErr1 + $txtNbComErr2)" | bc # works fine, but how can I store the result in a variable
(I found a solution using a file, but I don't find that clean...)
If anyone knows, I'm open to solutions :)
Configuration: Windows XP Firefox 1.5.0.12
4 answers
-
It's simpler and more efficient ;)
a=1
b=2
result=$(($a+$b))-
-
@arabesquedon't worry
-
Contributor@ProduPuissance4Since then, the water has flowed under the bridges, bash has leveled up, and the dollars ($) inside the braces have become obsolete ;-)
$ a=1; b=2; c=$((a+b)) && echo "$c"
3
-