[shell] How to add values

Solved
fabriceOb Posted messages 23 Status Member -  
zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   -
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 :)
Configuration: Windows XP Firefox 1.5.0.12

4 answers

  1. HRAF
     
    It's simpler and more efficient ;)
    a=1
    b=2
    result=$(($a+$b))
    28
    1. arabesque
       
      thanks the area
      0
      1. ProduPuissance4 > arabesque
         
        don't worry
        0
      2. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501 > ProduPuissance4
         
        Since 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
        1