Skip to main content

In addition to backticks `command`, command substitution can be done with $(command) or "$(command)", which I find easier to read, and allows for nesting.

OUTPUT="$(ls -1)" echo "${OUTPUT}" MULTILINE="$(ls \ -1)" echo "${MULTILINE}" 

Quoting (") does matter to preserve multi-line variable values and it is safer to use with whitespace and special characters such as (*) and therefore advised; it is, however, optional on the right-hand side of an assignment when word splitting is not performed, so OUTPUT=$(ls -1) would work fine.

Andy Lester
  • 94.2k
  • 16
  • 106
  • 162