Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2005-08-07 19:37:16
Size: 1076
Editor: GreyCat
Comment: copied and converted from heiner's wiki
Revision 11 as of 2015-11-12 10:59:10
Size: 3821
Comment:
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 one command into a second command. E.g. with an assignment:
Line 11: Line 11:
This can be used as part of other commands, e.g. This can also be used with other commands besides assignments:
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 WordSplitting, unless the whole thing is inside [[Quotes|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 [[SubShell|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
}}}

The exit status of the last command that was executed in the subshell is used as the exit status for the command substitution.
{{{
    $ var=$("non existent command")
    bash: non existent command: command not found
    $ echo $?
    127
}}}


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}
}}}
Line 28: Line 68:
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:
{{{
    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. [[BashFAQ/082|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

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

The exit status of the last command that was executed in the subshell is used as the exit status for the command substitution.

    $ var=$("non existent command")
    bash: non existent command: command not found
    $ echo $?
    127

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

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