Evaluation Order and eval
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, ;
, pipe, |
,
ampersand, &
, 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,
;
,(
,)
,<
,>
,|
, and&
. 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
vi ~$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 control-structure openers,{
, or(
), then the command is actually ...
Get Classic Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.