User avatar
paulwratt
Posts: 83
Joined: Fri Jun 12, 2015 12:15 am

verify EXPR bug in SHELL

Thu Nov 09, 2017 1:21 pm

can a few people verify a bug in different versions of Raspbian, on a console:

Code: Select all

expr 85 * 3
I dont use X, Bash is the $SHELL

I just verified this works:

Code: Select all

expr 85 \* 3
Cheers

Paul

jahboater
Posts: 4830
Joined: Wed Feb 04, 2015 6:38 pm

Re: verify EXPR bug in SHELL

Thu Nov 09, 2017 1:28 pm

Try doing

echo *

You will see that the shell expands * into all the visible filenames in the current directory.
\* stops that expansion.

There is no bug.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: verify EXPR bug in SHELL

Thu Nov 09, 2017 1:29 pm

This has nothing to do with BASH (other than that is common behavior in all Unix shells) or the Pi. It is a basic limitation of 'expr'. As you've deduced, you need to escape any arguments to 'expr' that are "special" to the shell. This includes, as you've noted, the * character.

'expr' is pretty much obsolete in the modern world. Back in the day, it was all we had, but there are better tools available now.

Using bash, the simplest way to do math in the shell is to use $(()) like this:

$ echo $((85*3))
255
$ foo=85;bar=3
$ echo $((foo*bar))
255
$

Now, if you are maintaining old code that uses 'expr', then the original authors would have been aware of these limitations and coded accordingly. If you are writing new code, don't use 'expr'.
Last edited by n67 on Thu Nov 09, 2017 1:30 pm, edited 1 time in total.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
DougieLawson
Posts: 36540
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: verify EXPR bug in SHELL

Thu Nov 09, 2017 1:30 pm

If you want to do complex maths in bash pipe it into bc with suitable use of apostropes and/or quotes.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
piglet
Posts: 911
Joined: Sat Aug 27, 2011 1:16 pm

Re: verify EXPR bug in SHELL

Thu Nov 09, 2017 2:50 pm

DougieLawson wrote:
Thu Nov 09, 2017 1:30 pm
If you want to do complex maths in bash pipe it into bc with suitable use of apostropes and/or quotes.
Yup: echo '(85 * 3)^2'| bc
65025

Return to “Raspbian”