Unexpected behavior of expr
Asked Answered
D

4

5

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,

Divulgence answered 31/1, 2010 at 10:14 Comment(0)
M
10

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

$ expr 3 '*' 2
6
Morbilli answered 31/1, 2010 at 10:16 Comment(0)
L
5

This should be enough:

expr 3 \* 2

Leyla answered 2/3, 2010 at 17:9 Comment(0)
H
3

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

Hardnosed answered 31/1, 2010 at 10:50 Comment(0)
W
2

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

Wisla answered 31/1, 2010 at 10:17 Comment(1)
you mean the contents of current directory?Divulgence

© 2022 - 2024 — McMap. All rights reserved.