[EnglishFrontPage] [TitleIndex] [WordIndex

Command Substitution

Command substitution is a very powerful concept of the UNIX shell. It is used to insert the output of one command into a second command. E.g. with an assignment:

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

This can also be used with other commands besides assignments:

    $ 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.

Of course, this could just be done with:

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

As with all substitutions, the results of a command substitution will undergo WordSplitting, unless the whole thing is inside double quotes.

Command substitutions may be nested within each other:

    IPs=($(awk /"$(</etc/myname)"/'{print $1}' /etc/hosts))

Notably, once inside a command substitution, the shell begins an entirely new quoting context. That is, double quotes inside the substitution do not match up with double quotes outside the substitution. So, things like this may be done:

    echo "The IPs are $(awk /"$(</etc/myname)"/'{print $1}' /etc/hosts | tr '\n' ' ')"

The outermost quotes delimit a single argument that will be passed to echo. The inner double quotes prevent word splitting or glob expansion on the results of the inner command substitution. The two sets of double quotes are independent of each other.

(For a better approach to discovering local IP addresses, see IpAddress.)

Command substitutions create subshells, so any changes to variables, current directory, etc. inside the command substitution affect only the rest of the substitution, and not the parent shell.

    $ var=$(cd ../../usr/bin; pwd)
    $ echo "$var"
    /usr/bin
    $ pwd
    /home/user

Command substitutions strip all trailing newlines from the output of the command inside them. This allows common cases such as foo=$(grep foo bar) to populate variables without needing a second step to remove the newline. Sometimes, you may want the newlines -- for example, when attempting to read an entire file into a variable without data loss (except NUL bytes):

    var=$(<file)   # strips trailing newlines

    # Workaround:
    var=$(cat file; printf x) var=${var%x}

Portability

The $(command) syntax is supported by KornShell, 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".

Nesting of command substitutions using the `...` syntax is more difficult. One must use backslashes:

    IPs_inna_string=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`
    
    # Very Bourne-ish: use the positional params as a pseudo array
    set -- `awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`

As one may imagine, this becomes rather unwieldy after two levels. FAQ 82 discusses the differences between $() and `` in more detail.

The use of $(<file) instead of $(cat file) is a Bashism that is slightly more efficient (doesn't require forking a cat(1) process) but obviously less portable.


CategoryShell


2012-07-01 04:05