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: revert to
   2 # non-standard mode where
   3 # echo '\a' outputs \a
   4 # instead ofa BEL character
   5 
   6 $ shopt -u xpg_echo
   7 $ echo "`echo \\a`"
   8 a
   9 
  10 $ echo "`echo \\\\a`"
  11 \a
  12 
  13 $ echo "$(echo \\a)"
  14 \\a
  15 
  16 # Note that this is true
  17 # for *single quotes* too!
  18 
  19 $ str=`echo '\\'`
  20 $ echo "str is $str"
  21 str is \
  22 
  23 $ str=$(echo '\\')
  24 $ echo "str is $str"
  25 str is \\

Other advantages

See also:

BashFAQ/082 (last edited 2025-05-26 12:16:02 by 81)