echo -e acts differently when run in a script by root on ubuntu

Posted by ekrub on Stack Overflow See other posts from Stack Overflow or by ekrub
Published on 2010-05-28T19:18:03Z Indexed on 2010/05/28 19:22 UTC
Read the original article Hit count: 254

Filed under:
|
|
|

When running a bash script on ubuntu 9.10, I get different behavior from bash echo's "-e" option depending on whether or not I'm running as root.

Consider this script:

$ cat echo-test
if [ "`whoami`" = "root" ]; then
  echo "Running as root" 
fi
echo Testing /bin/echo -e
/bin/echo -e "foo\nbar"
echo Testing bash echo -e
echo -e "foo\nbar"

When run as non-root user, I see this output:

$ ./echo-test 
Testing /bin/echo -e
foo
bar
Testing bash echo -e
foo
bar

When run as root, I see this output:

$ sudo ./echo-test 
Running as root
Testing /bin/echo -e
foo
bar
Testing bash echo -e
-e foo
bar

Notice the "-e" being echoed in the last case ("-e foo" instead of "foo" on the second-to-last line). When running a script as root, the echo command runs as if "-e" was given and, if -e is given, the option itself is echoed.

I can understand some subtle differences in behavior between /bin/echo and bash echo, but I would expect bash echo to behave the same no matter which user invokes it.

Anyone know why this is the case? Is this a bug in bash echo?

FYI -- I'm running GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)

© Stack Overflow or respective owner

Related posts about bash

Related posts about ubuntu