How to do it...

Pipes can be used with the subshell method for combining outputs of multiple commands.

  1. Let's start with combining two commands:
        $ ls | cat -n > out.txt

The output of ls (the listing of the current directory) is passed to cat -n, which in turn prepends line numbers to the input received through stdin. The output is redirected to out.txt.

  1. Assign the output of a sequence of commands to a variable:
        cmd_output=$(COMMANDS)

This is called the subshell method. Consider this example:

        cmd_output=$(ls | cat -n)
        echo $cmd_output

Another method, called back quotes (some people also refer to it as back tick) can also be used to store the command output:

        cmd_output=`COMMANDS`

Consider this example:

        cmd_output=`ls | cat -n`
        echo $cmd_output

Back quote is different from the single-quote character. It is the character on the ~ button on the keyboard.