Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected behavior of expr

It may sound a bit trivial but it actually is quite frustrating: Can anyone explain me this:


[~:user$]expr 3 + 2
6
[~:user$]expr 3 / 2
1
[~:user$]expr 3 * 2
expr: syntax error

When the man page for expr precisely specifies that ARG1 * ARG2 is arithmetic product of both.

Thanks,

like image 505
sud03r Avatar asked Jan 31 '10 10:01

sud03r


3 Answers

You need to quote the *, because otherwise the shell attempts file name expansion.

$ expr 3 '*' 2
6
like image 171
Peter Eisentraut Avatar answered Oct 15 '22 10:10

Peter Eisentraut


This should be enough:

expr 3 \* 2

like image 5
arancedisicilia Avatar answered Oct 15 '22 08:10

arancedisicilia


@OP, its entirely up to you, but i would still suggest to use shell internal addition operation instead of calling external expr

$ echo $(( 3*2 ))
6

If you require more advance maths operations, use bc(dc) or awk.

like image 3
ghostdog74 Avatar answered Oct 15 '22 09:10

ghostdog74