Differences between revisions 1 and 2
Revision 1 as of 2005-08-07 19:37:16
Size: 1076
Editor: GreyCat
Comment: copied and converted from heiner's wiki
Revision 2 as of 2008-05-08 23:48:50
Size: 2175
Editor: GreyCat
Comment: Expand and clean up
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
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. Command substitution 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.
Line 20: Line 20:
(note that this could just be done as:
Of course, this could just be done with:
Line 25: Line 24:
)
As with all substitutions, the results of a command substitution will undergo word splitting, unless the whole thing is inside "double quotes".

Command substitutions may be nested within each other:
{{{
    IP=$(awk "/$(cat /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 IP is $(awk "/$(cat /etc/myname)/"'{print $1}' /etc/hosts)"
}}}
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.
Line 28: Line 39:
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". 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:
{{{
    IP=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`
}}}
As one may imagine, this becomes rather unwieldy after two levels.

Command Substitution

Command substitution 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.

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 word splitting, unless the whole thing is inside "double quotes".

Command substitutions may be nested within each other:

    IP=$(awk "/$(cat /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 IP is $(awk "/$(cat /etc/myname)/"'{print $1}' /etc/hosts)"

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.

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:

    IP=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`

As one may imagine, this becomes rather unwieldy after two levels.

CommandSubstitution (last edited 2015-11-12 10:59:10 by AnthonyGeoghegan)