Reputation: 4607
Kindly tell me that is it necessary to use "expr" keyword.
EG:-
echo `expr a*b`
And where we can simply handle arithmetic expressions using simple arithmetic operators.
EG:-
echo a*b
Thanks in advance.
Upvotes: 3
Views: 13937
Reputation: 146053
In a Posix shell you can evaluate expressions directly in the shell when they are enclosed in
$(( ... ))
So:
a=12
b=34
echo $(($a + $b))
And although this wasn't always the case, all Posix shells you are likely to encounter will also deal with:
echo $((a + b))
This all happened because, a long time ago, the shell did not do arithmetic, and so the external program expr
was written. These days, expr
is usually a builtin (in addition to still being in /bin) and there is the Posix $((...))
syntax available. If $((...))
had been around from day one there would be no expr.
The shell is not exactly a normal computer language, and not exactly a macro processor: it's a CLI. It doesn't do inline expressions; an unquoted *
is a wildcard for filename matching, because a CLI needs to reference files more often than it needs to do arithmetic.
Upvotes: 15
Reputation: 477040
The second form will almost surely never do what you want. In Bash, you have a built-in numeric expression handler, though:
A=4; B=6; echo $((A * B))
Upvotes: 6