1

This is confounding. I need an explanation of the factor at work here which I'm apparently missing.

echo \\z

yields:

\z

echo \\\z

also yields:

\z

Ok fine (presumably because of the way backslashes in command line escaping are discarded even if the character being escaped would not have been treated specially anyway).

But

echo `echo \\z`

yields:

z

whereas

echo `echo \\\z`

yields:

\z

So, both command substitutions yield \z (when their command runs outside of a command substitution), but yet when run as command substitutions, when you echo the result of one of them, it produces a different result to echoing the result of the other.

WTF?

  • https://unix.stackexchange.com/questions/408507/can-someone-explain-the-strange-behavior-of-backslash-in-command-substitution – Arkadiusz Drabczyk Jan 15 '21 at 19:44
  • Escapes are processed weirdly inside of backticks before they're even passed to the `echo` command; use `$( )` instead. See [BashFAQ #82](http://mywiki.wooledge.org/BashFAQ/082) and [this question](https://stackoverflow.com/questions/34150970/why-can-back-quotes-and-for-command-substitution-result-in-different-output) – Gordon Davisson Jan 15 '21 at 19:45
  • Thanks for the pointers, I believe I see what's occurring now. But will indeed use `$( )` instead :). – MissingInfo Jan 16 '21 at 03:51

0 Answers0