Understanding Shell Parse Order
Shell Parse Order
Parsing controls how a shell processes a command line before it executes a command. A shell has a predefined order in which it interprets or evaluates different tokens (metacharacters, spaces, keywords, and so on) on the command line. Based on rules built into it, a shell interprets tokens in a specific order (the parse order) and then executes the command. The following list shows the parse order for the Bourne and Korn shells. The Korn shell has extra features, ignore those if you are reading this in preparation to use the Bourne shell.
- Read the command.
- Evaluate keywords. For example: if, else, fi, do, done, and time. You can verify these with “whence -v”.
- Evaluate the alias.
- Evaluate built-in commands. For example: alias, bg, cd, echo, and let. You can verify these with “whence -v”.
- Evaluate functions.
- Perform tilde expansion.
- Perform command substitution.
- Perform arithmetic expression substitution.
- Evaluate metacharacters for expansion of file names.
- Look up the command or script.
- Execute the command.
Special Characters Precedence
The special characters precedence is as follows:
- ’ (outside single quotes)
- ’’ (single quotes)
- \
- () {}
- ; &
- && ||
- |
- Variable substitution ` ` (back quotes)
- "" (double quotes)
- (( )) (calculations)
- * ? @() *() +() !()