Bash Shell Scripting/Pipelines And Substitution

As we have seen, a command's return value, taken strictly, is just a small non-negative integer intended to indicate success or failure. Its real output is what it writes to the standard output stream. By default, text written to the standard output stream is printed to the terminal, but there are a few ways that it can be "captured" and used as the command's true return value.

Pipelines
When a sequence of commands are linked together in a pipeline, the output of each command is passed as input to the next. This is a very powerful technique, since it lets us combine a number of small utility programs to create something complex.

Command substitution
Command substitution is a bit like variable expansion, but it runs a command and captures its output, rather than simply retrieving the value of a variable. For example, consider our  example above:

There is really no reason that the caller should have to save the password in a variable. If  simply printed the password to its standard output, then the caller could use command substitution, and use it directly:

To evaluate, Bash runs the command   in a subshell, capturing its standard output, and replaces   with the captured output.

In addition to the notation, an older notation   (using backquotes) is also supported, and still quite commonly found. The two notations have the same effect, but the syntax of  is more restrictive, and in complex cases it can be trickier to get right.

Command substitution allows nesting; something like  is allowed. (It runs the command, using its output as an argument to  , and using the output of that as an argument to  .)

Command substitutions can actually contain a sequence of commands, rather than just one. As we've seen previously, semicolons can be used instead of newlines to separate commands, and it is common to do this within command substitutions. Each of the semicolon-separated statements runs in sequence in a single subshell and their combined output is captured. Command substitutions can also contain variable assignments and function definitions, but these are useful only within the substitution, and do not persist in the parent shell once the substitution is finished.