Classic Shell Scripting - Arnold Robbins [93]
Checks the first word of each command against the list of aliases. If a match is found, it substitutes the alias's definition and goes back to step 1; otherwise it goes on to step 4. (Aliases are intended for interactive shells. As such, we haven't covered them here.) The return to step 1 allows aliases for keywords to be defined: e.g., alias aslongas=while or alias procedure=function. Note that the shell does not do recursive alias expansion: instead, it recognizes when an alias expands to the same command, and stops the potential recursion. Alias expansion can be inhibited by quoting any part of the word to be protected.
Substitutes the user's home directory ($HOME) for the tilde character (~) if it is at the beginning of a word. Substitutes user's home directory for ~ user.
Tilde substitution (in shells that support it) occurs at the following places: As the first unquoted character of a word on the command line
After the = in a variable assignment and after any : in the value of a variable assignment
For the word part of variable substitutions of the form ${ variable op word }
Performs parameter (variable) substitution for any expression that starts with a dollar sign ($).
Does command substitution for any expression of the form $( string ) or `string`.
Evaluates arithmetic expressions of the form $(( string )).
Takes the parts of the line that resulted from parameter, command, and arithmetic substitution and splits them into words again. This time it uses the characters in $IFS as delimiters instead of the set of metacharacters in step 1.
Normally, successive multiple input occurrences of characters in IFS act as a single delimiter, which is what you would expect. This is true only for whitespace characters, such as space and tab. For nonwhitespace characters, this is not true. For example, when reading the colon-separated fields of /etc/passwd, two successive colons delimit an empty field:while IFS=: read name passwd uid gid fullname homedir shell
do
...
done < /etc/passwd
Performs filename generation, a.k.a. wildcard expansion, for any occurrences of *, ?, and [...] pairs.
Uses the first word as a command following the search order described later in Section 7.9; i.e., as a special built-in command, then as a function, then as a regular built-in command, and finally as the first file found in a search of $PATH.
Runs the command after setting up I/O redirection and other such things.
As shown in Figure 7-1, quoting lets you bypass different parts of the evaluation process. On the flip side is the eval command, which lets you go through the process again. Performing command-line processing twice may seem strange, but it's actually quite powerful: it lets you write scripts that create command strings on the fly and then pass them to the shell for execution. This means that you can give scripts intelligence to modify their own behavior as they are running. (This is discussed further in the following section.)
Figure 7-1. Steps in command-line processing
The total sequence of steps shown in Figure 7-1 is pretty complicated. Each step happens inside the shell's memory as command lines are processed; it's not really possible to get the shell to show you each step as it happens. However, we can pretend to peek inside the shell's memory and see how the command line is transformed at each phase. We start with the following:
$ mkdir /tmp/x
Create temporary directory
$ cd /tmp/x
Change there
$ touch f1 f2
Create files for wildcarding
$ f=f y="a b"
Assign two variables
$ echo ~+/${f}[12] $y $(echo cmd subst) $((3 + 2)) > out
A busy command
Evaluation proceeds in the steps outlined previously:
The command is first split into tokens based