Shell – Should `…` Substitution Be Used in New Development?

command-substitutionshell

It is my understanding that the more modern $(...) command substitution syntax is preferred over the old `-based syntax, due to easier and less error-prone nesting and escaping syntax.

Further, it seems that most /bin/sh-style shells in modern use support $(…):

  • bash
  • ash (and therefore BusyBox, so most embedded Linux)
  • dash
  • FreeBSD /bin/sh

And $(…) is specified by IEEE 1003.1.

So I have 2 very related questions:

  • Is there any reason to use ` in new development of shell scripts unless you know of a specific old system that the script will need to run on?
  • Is there any reason not to teach UNIX programming students just to write $(...), and discuss ` only as an obsolete variant that they will likely encounter if they are reading other developers' shell scripts (and may need if they are working with a really old system or nonstandard for some reason)?

Best Answer

Since back-ticks are often used, it makes sense to teach this syntactic construct.

Of course, $() style command substitution should be emphasized as the default style (and standard conforming construct).

Why are back-ticks still popular? Because they save one character in typing, and they are arguably less heavy on the eye.

Related Question