11

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

wheleph
  • 7,974
  • 7
  • 40
  • 57

10 Answers10

15

The one true way to print any arbitrary string:

printf "%s" "$vars"
nanaya
  • 500
  • 4
  • 5
  • Wouldn't it be nice? But this won't work for embedded ANSI Escape sequences, and depends on who the value was assigned. Try `v="$(tput sgr0)"; printf "%s" "$v"` or `v=$(printf '\033[4m'); printf "%s" "$v"`etc. – Andreas Spindler Feb 14 '13 at 10:16
  • @AndreasSpindler, if you want `%b` rather than `%s`... well, it's available in all POSIX printf implementations, and that gives you the same escape-sequence handling as found in `echo`. – Charles Duffy Nov 16 '14 at 16:50
13

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
5

There may be a better way, but this works:

printf -- "-e\n"
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • In this case it works but it doesn't solve the global problem. Consider: printf "--version" – wheleph Nov 26 '08 at 12:25
  • 2
    @wheleph You have ignored the "--" before the "-e\n". This separates options from arguments. This is the best, portable (across shells) answer. +1. – richq Nov 26 '08 at 13:03
  • I agree that `printf` is the best, portable answer -- but disagree (strongly!) that embedding data in the format string is the right way to teach its use; that way lies people writing `printf -- "$var\n"` when they should be using `printf '%s\n' "$var"`, and that way lies format string injections (which, while not necessarily security-impacting in bash, can certainly lead to annoying bugs). – Charles Duffy Nov 16 '14 at 16:48
4

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

I like that one using a herestring:

cat <<<"-e"
Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
1
 
[root@scintia mail]# POSIXLY_CORRECT=1; export POSIXLY_CORRECT
[root@scintia mail]# /bin/echo "-e"
-e
[root@scintia mail]#
azerole
  • 1,262
  • 3
  • 16
  • 23
  • Just 'POSIXLY_CORRECT=1 /bin/echo -e' (without the 's) is enough. Although this is not using the BASH version of echo – Vinko Vrsalovic Nov 26 '08 at 11:16
  • 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 Nov 26 '08 at 16:06
  • Vinko, '/bin/echo -e' is also enough:) – wheleph Nov 26 '08 at 16:14
1

After paying careful attention to the man page :)

SYSV3=1 /usr/bin/echo -e

works, on Solaris at least

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
1

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
  • 43,947
  • 7
  • 63
  • 86
0

Another way:

echo -e' '
echo -e " \b-e"
FerranB
  • 35,683
  • 18
  • 66
  • 85
-2
/bin/echo -e

works, but why?

[resin@nevada ~]$ which echo 
/bin/echo
wheleph
  • 7,974
  • 7
  • 40
  • 57
  • 2
    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 Dec 11 '08 at 08:48
  • 2
    `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 Apr 06 '10 at 04:48