tags:

views:

1789

answers:

9

I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command:

echo "-e"

doesn't print anything. I guess this is because "-e" is one of a valid options of echo command because echo "-n" and echo "-E" (the other two options) also produce empty strings.

The question is how to escape the sequence "-e" for echo to get the natural output ("-e").

+2  A: 

You could cheat by doing

echo "-e "

That would be dash, e, space.

Alternatively you can use the more complex, but more precise:

echo -e \\x2De

Joachim Sauer
+5  A: 

This is a tough one ;)

Usually you would use double dashes to tell the command that it should stop interpreting options, but echo will only output those:

$ echo -- -e
-- -e

You can use -e itself to get around the problem:

$ echo -e '\055e'
-e

Also, as others have pointed out, if you don't insist on using the bash builtin echo, your /bin/echo binary might be the GNU version of the tool (check the man page) and thus understand the POSIXLY_CORRECT environment variable:

$ POSIXLY_CORRECT=1 /bin/echo -e
-e
hop
@hop: Do you happen to know, why `echo -e` does not work from Makefiles?
dma_k
@dma_k: works for me(tm). make(1) does not use the $SHELL builtin you are using, so it will behave differently.
hop
@hop: Thanks, great! Indeed, setting `SHELL=/bin/bash` in Makefile solved the problem!
dma_k
+4  A: 

There may be a better way, but this works:

printf -- "-e\n"
Stephen Darlington
In this case it works but it doesn't solve the global problem. Consider: printf "--version"
wheleph
@wheleph You have ignored the "--" before the "-e\n". This separates options from arguments. This is the best, portable (across shells) answer. +1.
rq
@rq Yes, I've missed it. +1
wheleph
+1  A: 
 
[root@scintia mail]# POSIXLY_CORRECT=1; export POSIXLY_CORRECT
[root@scintia mail]# /bin/echo "-e"
-e
[root@scintia mail]#
azerole
Just 'POSIXLY_CORRECT=1 /bin/echo -e' (without the 's) is enough. Although this is not using the BASH version of echo
Vinko Vrsalovic
Information about Bash POSIX mode: http://www.network-theory.co.uk/docs/bashref/BashPOSIXMode.html. Item 41 addresses echo command. The only question left for me is why [root@scintia mail]# POSIXLY_CORRECT=1; export POSIXLY_CORRECT; echo "-e"; doesn't work
wheleph
Vinko, '/bin/echo -e' is also enough:)
wheleph
A: 

Another way:

echo -e' '
echo -e " \b-e"
FerranB
tho it outputs more than just "-e" under the hood :)
Johannes Schaub - litb
A: 

After paying careful attention to the man page :)

SYSV3=1 /usr/bin/echo -e

works, on Solaris at least

Paul.

Paul
I haven't found any references to SYSV3 env variable in Linux echo man page
wheleph
Looks like the POSIXLY_CORRECT env var from another answer is the equivalent?
Paul
A: 
/bin/echo -e

works, but why?

[resin@nevada ~]$ which echo 
/bin/echo
wheleph
If you write "echo -e" it uses the bash internal echo, and writing "/bin/echo -e" uses the external /bin/echo command. But on my Ubuntu 8.04 box none of the two versions work..
Anders Westrup
`which` will tell you which binary will be used, but it won't tell if your shell has a built-in function of the same name (which would probably be used instead of the binary). You can use `type` instead of `which` to find out more information.
dreamlax
+1  A: 

Another alternative:

echo x-e | sed 's/^x//'

This is the way recommended by the autoconf manual:

[...] It is often possible to avoid this problem using 'echo "x$word"', taking the 'x' into account later in the pipe.

CesarB
A: 

The one true way to print any arbitrary string:

printf "%s" "$vars"
edogawaconan