Unexpected expr behavior

This may seem a little trivial, but actually it’s rather complicated: Can someone explain this to me:

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

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

Thanks,

+4
source share
4 answers

You need to quote * , because otherwise the shell tries to decrypt the file name.

 $ expr 3 '*' 2 6 
+8
source

That should be enough:

expr 3 \ * 2

+4
source

* expands with your shell until expr reached, so it contains Documents , Videos , Work , nsfw , etc.

+2
source

@OP, it is entirely up to you, but I still suggest using the internal shell add operation instead of calling the external expr

 $ echo $(( 3*2 )) 6 

If you need more math operations, use bc (dc) or awk.

+2
source

Source: https://habr.com/ru/post/1299868/


All Articles