Sure, echo -e can be used so that \n is understood as a new line. The problem is when I want to echo something beginning with \t e.g. "\test".

So let's say I want to perform echo -e "test\n\\test". I expect this to output:


But instead outputs:


The \\t is being interpreted as a tab instead of a literal \t. Is there a clean workaround for this issue?

  • 2
    Use single quotes and avoid echo. printf 'test\n\\test\n' or printf 'test\n%s\n' '\test'. – Stéphane Chazelas Mar 2 '17 at 22:54
  • Sure, thanks. Is an echo solution possible? – Zeruno Mar 2 '17 at 23:13
echo -e "\\t"

passes \t to echo because backslash is special inside double-quotes in bash. It serves as an escaping (quoting) operator. In \\, it escapes itself.

You can either do:

echo -e "\\\\t"

for echo to be passed \\t (echo -e "\\\t" would also do), or you could use single quotes within which \ is not special:

echo -e '\t'

Now, echo is a very unportable command. Even in bash, its behaviour can depend on the environment. I'd would advise to avoid it and use printf instead, with which you can do:

printf 'test\n\\test\n'

Or even decide which parts undergo those escape sequence expansions:

printf 'test\n%s\n' '\test'


printf '%b%s\n' 'test\n' '\test'

%b understands the same escape sequences as echo (some echos), while the first argument to printf, the format, also understands sequences, but in a slightly different way than echo (more like what is done in other languages). In any case \n is understood by both.


This is not in accordance with manual for the command, but man itself states point 1:

  1. Echo depends on your shell (and there are other reasons sometimes to avoid it, like piping).
  2. Stéphane's comment is not a bad way to go: printf it.
  3. echo solution echo -e "a\na\ta" (double quotes, read up on what they do though if you want to expand that code for something more) and tests:


➜  ~ echo -e a \n a \ta
a n a ta
➜  ~ echo -e "a \n a \ta"
 a  a
➜  ~ echo -e "a\na\ta"   
a   a
➜  ~ bash                
T420s:~$ echo -e "a\na\ta"
a   a
T420s:~$ echo -e a\na\ta
T420s:~$ echo a\na\ta
T420s:~$ echo "a\na\ta"

First shell is ZSH.


The shell snippet "test\n\\test" expands to the string test\n\test, because a backslash inside double quotes causes the next character to be interpreted literally if the next character is one of "$\` and otherwise the backslash itself is interpreted literally. Then echo -e replaces \n with a newline and \t with a tab.

Avoid double quotes to quote literal text. Use single quotes instead: they have simpler rules — everything inside single quotes is interpreted literally is a single quote. If you choose to use double quotes, don't use backslashes that are not followed by a character that it would quote — if you want a backslash inside double quotes, use \\.

echo -e 'test\n\\test'
echo -e "test\\n\\\\test"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.