Command substitutions

Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution occurs when the command is enclosed as follows:

$(command)

or by using backquotes:

`command`

The backquoted version is provided for compatibility. Its use is discouraged.

The shell expands the command substitution by running command in a subshell environment and replacing the command substitution with the standard output of the command, removing sequences of one or more <newline>s at the end of the substitution. Embedded <newline>s before the end of the output are not removed; however, during field splitting, they may be translated into <space>s, depending on the value of the IFS variable and quoting that is in effect.

Examples

  1. Set the variable list to the output of the ls command:
    list=$(ls)