Command Substitution

CommandSubstitution is a very powerful concept of the UNIX shell. It is used to insert the output of a command in a certain place, e.g.

    $ today=$(date)        # starts the "date" command, captures its output
    $ echo "$today"
    Mon Jul 26 13:16:02 MEST 2004

This can be used as part of other commands, e.g.

    $ echo "Today is $(date +%A), it's $(date +%H:%M)"
    Today is Monday, it's 13:21

This calls the date command two times, the first time to print the week-day, the second time for the current time.

(note that this could just be done as:

    date "+Today is %A, it's %H:%M"

)

Portability

The $(command) syntax is supported by KornShell, ["KornShell93"], ["BASH"], and PosixShell. Older shells (e.g. BourneShell) use the following syntax: `command`. Note that these are not the apostrophe characters '...', but small ticks going from the upper left to the lower right: `...`. These are often called "backticks" or "back quotes".