From Learning the Bash Shell by Newham:

Each line that the shell reads from the standard input or a script is called a pipeline; it contains one or more commands separated by zero or more pipe characters (|). For each pipeline it reads, the shell breaks it up into commands, sets up the I/O for the pipeline, then does the following for each command (Figure 7-1):

  1. Splits the command into tokens that are separated by the fixed set of metacharacters: SPACE, TAB, NEWLINE, ;, (, ), <, >, |, and &. Types of tokens include words, keywords, I/O redirectors, and semicolons.
  1. After the shell breaks a pipeline into commands separated by |, why is | still listed as a metacharacter which separate tokens in each command? Can | appear in each command?

  2. The Bash Manual says that when a bash shell runs a pipeline, it forks a subshell to run each command in a pipeline.

    For each command in a pipeline, which shell "does the following for each command": the subshell forked for the command, or the original shell?

1 Answer 1

  • The pipe character | is a meta character because it ends a word that is not quoted. This is needed to make the shell language easy to understand.

  • The way shells create the various processes for a pipeline is not standardized and differs between implementations.

The Bourne Shell originally did create a sub-shell that then becomes the parent of all processes of a pipeline and finally starts the rightmost program in the pipeline. This is the method that needs less code than other methods and that permits to implement job-control simple.

Bash works in a similar way.

Ksh93 makes every process from a pipeline a direct child of the original shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .