Reputation: 131
I have a shell program as follow:
#!/bin/sh
a=1
b=2
c=`expr $a+$b`
echo $c
The output is 1+2
.
But if I change
c=`expr $a+$b\`
to
c=`expr $a + $b`
The output is going to be 3.
I wonder what is the use of space here.
Upvotes: 0
Views: 145
Reputation: 19163
expr
is a shell command expecting arguments of two types: operands (the "numbers") and operators (+, -, *, etc.). In your case, you want to pass three arguments to expr
, which are $a, +, and $b. The dollar sign values are expanded into their actual values of 1 and 2 by your shell, and not by expr
. So, when you do expr $a + $b
, the actual shell command is expr 1 + 2
, which gives you 3.
When you don't put spaces in, what gets executed is expr 1+2
. The expr
program doesn't know what to do with 1+2
as its sole argument, and thus just echoes it back untouched.
Upvotes: 2