How does echo `echo \\\\\\\z`
result in \\z
?
Syntax tree looks like this:
- echo
-> `
-> echo
-> \\\\\\\z
We start from \\\\\\\z
. Firstly https://www.gnu.org/software/bash/manual/html_node/Escape-Character.html#Escape-Character happens:
A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows [...]
The \\\\\\z
is unquoted, so we transform each pair \?
to ?
, like \\
into \
and \z
into z
.
\\\\\\\z
^^|||||| -> \
^^|||| -> \
^^|| -> \
^^ -> z
- echo
-> `
-> echo
-> \\\z
Then `...`
backquotes happen, one of the reasons why $(..)
is preferred for command substitution. From https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution :
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘`’, or ‘\’.
In \\\z
the first \
is followed by \
, so it's meaning is not literal. The meaning of the first \
is, as in the previous point, it preserves the literal value of the next character. So, basically, \\
is replaced by a single \
. The rest \z
is preserved literally.
\\\z
^^ -> \
.. -> \z
- echo
-> `
-> echo
-> \\z
Then echo '\\z'
is executed, and it outputs \\z
and the command substitution `...`
is replaced by the output of the command inside, so, well, by \\z
.
- echo
-> \\z
Then echo '\\z'
once again outputs \\z
.