0

I found this:

echo `echo \\n`

output is n

while

echo $(echo \\n)

output is \n

I knew $() can perform nesting while `` can't, but there seems to be other differences.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
陈守龙
  • 1
  • 1
  • 2
    You might want to read https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03 for the standard's definition of command substitution, in particular its handling of backslashes. Roughly speaking, the backquoted form replaces the double backslash with a single backslash *before* the command inside backquotes is parsed and execute. – chepner Jan 11 '23 at 17:01
  • Backticks can be nested, they just need to be backslash-escaped. But that means backslashes have a special meaning inside backticks, so they need to be doubled to have their original non-special meaning. – Charles Duffy Jan 11 '23 at 18:34

2 Answers2

0

The backquote is used in the old-style command substitution, e.g.

foo=`command`

The

foo=$(command)

syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    While all that is good advice, it doesn't explain why OP is seeing the behavior they're asking about. – Shawn Jan 11 '23 at 18:05
0
  1. About `...` syntax In:
echo `echo \\n`
  • `echo \\n` return \n text
  • echo \n returns n
    • It's really different than echo -e "\n" who returns a ASCII LF character
  1. About $(...) syntax In:
echo $(echo \\n)
  • `echo \\n` return \n text but with $(...) returned like ONE token "\n"
  • echo "\n" returns \n
Arnaud Valmary
  • 2,039
  • 9
  • 14