1

I have written the following script file from my book.

a=2
b=3 
c=$[a+b]
echo 'The result is:' $c

After executing this program, instead of getting 5, I am getting output as:

The result is: $[a+b]
9
  • It should work in bash. Try putting #!/bin/bash at the start of your script.
    – Jos
    Aug 9, 2014 at 18:46
  • @Jos try it :) I thought so too... It is now depreciated, and so i guess that is why it wouldn't work... stackoverflow.com/questions/6348902/…
    – Tim
    Aug 9, 2014 at 18:47
  • Works for me, @Tim. Yes I tried it first.
    – Jos
    Aug 9, 2014 at 18:48
  • @Jos Event after adding the quoted string at the start, the output remained the same.
    – Mistu4u
    Aug 9, 2014 at 18:50
  • @Jos, oh so it does, miss type... OP: Are you doing this in a bash script or a terminal?
    – Tim
    Aug 9, 2014 at 18:50

1 Answer 1

1

You shouldn't be using []:

Go for this (with the (()) brackets:

a=2
b=3
c=$((a+b))
echo 'The result is:' $c

returns:

The result is: 5
1
  • This is working.
    – Mistu4u
    Aug 9, 2014 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .