The various expansions and substitutions that we've covered are done in a defined order. The POSIX standard provides the picayune details. Here, we describe things at the level a shell programmer needs to understand things. This explanation is simplified to elide the most petty details: e.g., middles and ends of compound commands, special characters, etc.
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
|). (Actually, several special
symbols separate individual commands: semicolon,
&, logical AND,
&&, and logical OR,
||.) For each pipeline it reads, the shell
breaks it up into commands, sets up the I/O for the pipeline, and then
does the following for each command, in the order shown:
Splits the command into tokens that are
separated by the fixed set of metacharacters:
space, tab, newline,
&. Types of tokens include
words, keywords, I/O
redirectors, and semicolons.
It's a subtle point, but variable, command, and arithmetic
substitution can be performed while the shell is doing token
recognition. This is why the
~$user/.profile example presented earlier in Section 7.5.1, actually
works as expected.
Checks the first token of each command to see if it is a
keyword with no quotes or backslashes. If it's
an opening keyword (
if and other
(), then the command is actually ...