3

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?

Fravadona
  • 13,917
  • 1
  • 23
  • 35

2 Answers2

4

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.

nos
  • 223,662
  • 58
  • 417
  • 506
  • 2
    It's true that it is an old option (bash 3.2 have it), but it's my first time getting it turned on by default... – Fravadona Apr 18 '23 at 19:40
  • 1
    @Fravadona There might be a .bashrc/.bash_profile for your user or globally on that particular machine that turns it on – nos Apr 19 '23 at 09:21
2

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?.

pjh
  • 6,388
  • 2
  • 16
  • 17