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?
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.
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?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With