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.

  1. Read the command.
  2. Evaluate keywords. For example: if, else, fi, do, done, and time. You can verify these with “whence -v”.
  3. Evaluate the alias.
  4. Evaluate built-in commands. For example: alias, bg, cd, echo, and let. You can verify these with “whence -v”.
  5. Evaluate functions.
  6. Perform tilde expansion.
  7. Perform command substitution.
  8. Perform arithmetic expression substitution.
  9. Evaluate metacharacters for expansion of file names.
  10. Look up the command or script.
  11. Execute the command.

Special Characters Precedence

The special characters precedence is as follows:

  1. ’ (outside single quotes)
  2. ’’ (single quotes)
  3. \
  4. () {}
  5. ; &
  6. && ||
  7. |
  8. Variable substitution ` ` (back quotes)
  9. "" (double quotes)
  10. (( )) (calculations)
  11. * ? @() *() +() !()