views:

48

answers:

4

Hi all,
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,

+5  A: 

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

$ expr 3 '*' 2
6
Peter Eisentraut
+1  A: 

* is expanded by your shell before it reaches expr, therefore it contains Documents, Videos, Work, nsfw, etc.

Ignacio Vazquez-Abrams
you mean the contents of current directory?
Neeraj
That is correct.
Ignacio Vazquez-Abrams
+2  A: 

@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.

ghostdog74
A: 

This should be enough:

expr 3 \* 2

arancedisicilia