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:
test \test
But instead outputs:
test est
The \\t
is being interpreted as a tab instead of a literal \t. Is there a clean workaround for this issue?
Best Answer
passes
\t
toecho
because backslash is special inside double-quotes inbash
. It serves as an escaping (quoting) operator. In\\
, it escapes itself.You can either do:
for
echo
to be passed\\t
(echo -e "\\\t"
would also do), or you could use single quotes within which\
is not special:Now,
echo
is a very unportable command. Even inbash
, its behaviour can depend on the environment. I'd would advise to avoid it and useprintf
instead, with which you can do:Or even decide which parts undergo those escape sequence expansions:
Or:
%b
understands the same escape sequences asecho
(someecho
s), while the first argument toprintf
, the format, also understands sequences, but in a slightly different way thanecho
(more like what is done in other languages). In any case\n
is understood by both.