Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "echo" operate like "echo -e" in my bash?

Tags:

bash

echo

I stumbled upon a weird problem with echo on GNU bash, version 5.1.12(1)-release (x86_64-pc-solaris2.11):

bash-5.1$ builtin echo -e 'a\tb'
a       b

bash-5.1$ builtin echo 'a\tb'
a       b

bash-5.1$ builtin echo +e 'a\tb'
+e a    b

While it won't affect my scripts because I mostly use printf, how do you turn echo -e off in bash?

like image 398
Fravadona Avatar asked Feb 11 '26 12:02

Fravadona


2 Answers

You have the xpg_echo shell option enabled.

List the options by running shopt

Disable it with shopt -u xpg_echo

To figure this out, read the echo section of man builtins (assuming that gives you the bash builtin commands, otherwise look at https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html)

The relevant part is

The xpg_echo shell option may be used to dynamically determine whether or not echo expands these escape characters by default.

like image 131
nos Avatar answered Feb 16 '26 08:02

nos


Since there are many possible ways for the xpg_echo option to get turned on, the safest option when you want to ensure that echo will not interpret backslash escapes may be to always use the -E option. However, code like

echo -E "$var"

still doesn't work in general because "$var" might expand to an echo option. I find it best to use echo only for simple literal strings, if at all. Also see the accepted, and excellent, answer to Why is printf better than echo?.

like image 30
pjh Avatar answered Feb 16 '26 06:02

pjh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!