Command-Line Processing Steps

We’ve touched upon command-line processing throughout this book; we’ve mentioned how bash deals with single quotes (''), double quotes (“;”), and backslashes (\); how it separates characters on a line into words, even allowing you to specify the delimiter it uses via the environment variable $IFS; how it assigns the words to shell variables (e.g., $1, $2, etc); and how it can redirect input and output to/from files or to other processes (pipeline). In order to be a real expert at shell scripting (or to debug some gnarly problems), you might need to understand the various steps involved in command-line processing—especially the order in which they occur.

Each line that the shell reads from STDIN or from a script is called a pipeline because it contains one or more commands separated by zero or more pipe characters (|). Figure C-1 shows the steps in command-line processing. For each pipeline it reads, the shell breaks it up into commands, sets up the I/O for the pipeline, then does the following for each command.

  1. 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.

  2. 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 such as if and other control-structure openers, function , {,or (, then the command is actually a compound command. The ...

Get bash Cookbook 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.