I ran echo "\oo" | tr "\oo" " "
, and the output is:
\
\o
is not a special character, like \n
, so why is the \
not converted into a space too?
I ran echo "\oo" | tr "\oo" " "
, and the output is:
\
\o
is not a special character, like \n
, so why is the \
not converted into a space too?
Because the shell and GNU tr appear to deal with unknown backslash sequences differently.
When the shell sees
"\oo"
, it leaves the backslash as-is (I think that's the required standard behaviour). But whentr
consequently gets\oo
as its first argument, it interprets the\o
as justo
. Busybox'str
deals with this differently, this prints only spacesecho "\oo" | busybox tr "\oo" " "
.Conclusion, don't rely on undefined backslash sequences, but escape the backslashes when they are meant to be taken literally.
E.g. use
echo '\oo' | tr '\\o' ' '
instead. (The single quotes protect the backslash from the shell, the second backslash escapes the other fortr
.)This question is essentially "how do you escape the escape '\' char?"
Answer: The same way you escape all other chars.