[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug in "expr"
From: |
Bob Proulx |
Subject: |
Re: Bug in "expr" |
Date: |
Wed, 16 Dec 2009 18:52:21 -0700 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
Jay McGuerty wrote:
> I am using CentOS v5.3 BASH v3.2.25(1) and get an error when I use
> the "expr" command to extract characters and it encounters a space.
> It returns the correct value - just sends an error as well..... this
> appears to be a bug....
Thanks for the report. However I think this is a misunderstanding of
how the shell handles empty variable expansion.
> # echo $slime
> three blind mice
> char=`expr ${slime:4:1}`
> # echo $char
> e
And also:
$ echo ${slime:4:1}
e
$ expr e
e
> # char=`expr ${slime:5:1}`
> expr: missing operand
> Try `expr --help' for more information.
This is expected because the output of ${slime:5:1} is empty.
$ echo ${slime:5:1}
...no output...
$ echo ${slime:5:1} | od -tx1
0000000 0a
0000000
Because the variable was empty it did not produce any argument to expr
at all.
$ expr
expr: missing operand
Try `expr --help' for more information.
So as you can see the expr command is behaving as expected.
If you want to preserve whitespace in variables then you will need to
quote the argument. It is hard to see space output so I will invoke
the help of od to show the binary results as hex output.
$ echo "${slime:5:1}"
$ echo "${slime:5:1}" | od -tx1
0000000 20 0a
0000000
$ expr "${slime:5:1}" | od -tx1
0000000 20 0a
0000002
Meanwhile, I am not sure that using expr to echo arguments is the best
use case. That just seems odd to me. And produces the possibility of
data dependent errors depending upon the contents of the variable. I
expect that you are trying to do something different though.
Hope that helps,
Bob
- Bug in "expr", Jay McGuerty, 2009/12/16
- Re: Bug in "expr",
Bob Proulx <=