Differences between revisions 6 and 7
Revision 6 as of 2010-09-01 01:41:07
Size: 3331
Editor: GreyCat
Comment:
Revision 7 as of 2011-06-03 08:32:44
Size: 3325
Editor: neurolysis
Comment: poor example. also does not adhere to varcap and uuoc prevention.
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:
    IP=$(awk "/$(cat /etc/myname)/"'{print $1}' /etc/hosts)     ip=$(awk "/$(</etc/myname)/"'{print $1}' /etc/hosts)
Line 34: Line 34:
    echo "The IP is $(awk "/$(cat /etc/myname)/"'{print $1}' /etc/hosts)"     echo "The IP is $(awk "/$(</etc/myname)/"'{print $1}' /etc/hosts)"

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

Command substitutions may be nested within each other:

    ip=$(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 IP is $(awk "/$(</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.

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=$(cat file)   # strips trailing newlines

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

Bash also offers a shortcut for $(cat file) -- namely, $(< file). If you want a whole file in a variable, and don't care about losing trailing newlines, this is a tiny bit more efficient than calling cat.

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. FAQ 82 discusses the differences between $() and `` in more detail.


CategoryShell

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