You can chain a set of commands together:
You can use the | character to combine two commands to form a pipeline.
You can use the characters; or & or && or || to combine multiple commands or pipelines to form a command list.
Pipeline
A pipeline is a sequence of two or more commands each of which is linked to the preceding command by means of the | character. A pipeline between two commands redirects the standard output of the first command to the standard input of the second command (see the section “I/O redirection”). For this reason, commands which are chained in this way must fulfil the following conditions:
The first command must write to the standard output.
The next command in the pipeline must read from the standard input.
If the pipeline contains more than two commands, then all the commands which come between the first and last command of the pipeline must read from the standard input and write to the standard output.
The POSIX shell starts all of the piped commands as independent parallel processes. However, in a monoprocessor system, the processor can only handle one of these processes during each time slice. For this reason, processor time is devoted to the individual processes in turn.
Only the process corresponding to the first command in the pipeline does not receive its input from another process.
Only the process corresponding to the last command in the pipeline does not route its output to another process.
The POSIX shell waits until the last of the piped commands terminates before processing any further inputs.
Command list
A command list is a sequence of one or more pipelines separated by the symbols
; & && ||
and optionally terminated by
; & |&
The symbols, ;, &, and |& have equal precedence, which is lower than that of && and ||. The symbols && and || also have equal precedence. A semicolon (;) causes sequential execution of the preceding pipeline; an ampersand (&) causes asynchronous execution of the preceding pipeline (i.e. the shell does not wait for that pipeline to finish). The symbol |& after a command list causes asynchronous execution of the preceding command or pipeline with a two-way pipe established to the parent shell.
In the following, the term command refers to a command or a pipeline. The effect of the various characters is specified in the table below:
Character | Effect |
; | The POSIX shell does not process the next command until the preceding command has terminated. It does not, however, issue a prompt. |
& | The POSIX shell starts the preceding command in the background and then immediately processes the following command. |
&& | The POSIX shell does not execute the following command unless the preceding command returns the value 0 as its exit status. |
|| | The POSIX shell does not execute the following command unless the preceding command returns a value other than 0 as its exit status. |
|& | The POSIX shell starts the preceding command in the background with a two-way pipeline to the parent shell. |