echo "-e" doesn't print anything

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").

Answers


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

The one true way to print any arbitrary string:

printf "%s" "$vars"

There may be a better way, but this works:

printf -- "-e\n"

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

 
[root@scintia mail]# POSIXLY_CORRECT=1; export POSIXLY_CORRECT
[root@scintia mail]# /bin/echo "-e"
-e
[root@scintia mail]#

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.


After paying careful attention to the man page :)

SYSV3=1 /usr/bin/echo -e

works, on Solaris at least


I like that one using a herestring:

cat <<<"-e"

Another way:

echo -e' '
echo -e " \b-e"

/bin/echo -e

works, but why?

[resin@nevada ~]$ which echo 
/bin/echo

Need Your Help

What is more efficient: System.arraycopy or Arrays.copyOf?

java arrays performance

The toArray method in ArrayList, Bloch uses both System.arraycopy and Arrays.copyOf to copy an array.

How to add animation to a text view in android

android animation fadein

I have a TextView and I'm trying to add a fade in animation to it. My code is returning null and I don't understand why.