Why is $(...) preferred over `...` (backticks)?

The `cmd` backtick format is the legacy syntax for command substitution, required only by more than 30-year-old Bourne shells. The modern POSIX $(...) syntax is preferred for many reasons:

Important differences

   1 $ shopt -u xpg_echo # revert to non-standard mode where echo '\a' outputs \a instead of a BEL character
   2 $ echo "`echo \\a`"
   3 a
   4 $ echo "`echo \\\\a`"
   5 \a 
   6 $ echo "$(echo \\a)"
   7 \\a
   8 
   9 # Note that this is true
  10 # for *single quotes* too!
  11 $ str=`echo '\\'`
  12 $ echo "str is $str"
  13 str is \
  14 $ str=$(echo '\\')
  15 $ echo "str is $str"
  16 str is \\

Other advantages

See also:

BashFAQ/082 (last edited 2025-04-27 08:00:55 by StephaneChazelas)