Chapter 3: Basic Shell Features

5

3 Basic Shell Features

Bash is an acronym for ‘Bourne-Again SHell’. The Bourne shell is the traditional Unix shell originally written by Stephen Bourne. All of the Bourne shell builtin commands are available in Bash, The rules for evaluation and quoting are taken from the posix specification for the

‘standard’ Unix shell.

This chapter briefly summarizes the shell’s ‘building blocks’: commands, control struc- tures, shell functions, shell parameters, shell expansions, redirections, which are a way to direct input and output from and to named files, and how the shell executes commands.

3.1 Shell Syntax

When the shell reads input, it proceeds through a sequence of operations. If the input indicates the beginning of a comment, the shell ignores the comment symbol (‘#’), and the rest of that line.

Otherwise, roughly speaking, the shell reads its input and divides the input into words and operators, employing the quoting rules to select which meanings to assign various words and characters.

The shell then parses these tokens into commands and other constructs, removes the special meaning of certain words or characters, expands others, redirects input and output as needed, executes the specified command, waits for the command’s exit status, and makes that exit status available for further inspection or processing.

3.1.1 Shell Operation

The following is a brief description of the shell’s operation when it reads and executes a command. Basically, the shell does the following:

1. Reads its input from a file (see Section 3.8 [Shell Scripts], page 39), from a string supplied as an argument to the -c invocation option (see Section 6.1 [Invoking Bash], page 80), or from the user’s terminal.

2. Breaks the input into words and operators, obeying the quoting rules described in

Section 3.1.2 [Quoting], page 6. These tokens are separated by metacharacters. Alias expansion is performed by this step (see Section 6.6 [Aliases], page 88).

3. Parses the tokens into simple and compound commands (see Section 3.2 [Shell Com- mands], page 8).

4. Performs the various shell expansions (see Section 3.5 [Shell Expansions], page 21), breaking the expanded tokens into lists of filenames (see Section 3.5.8 [Filename Ex- pansion], page 30) and commands and arguments.

5. Performs any necessary redirections (see Section 3.6 [Redirections], page 31) and re- moves the redirection operators and their operands from the argument list.

6. Executes the command (see Section 3.7 [Executing Commands], page 35).

7. Optionally waits for the command to complete and collects its exit status (see

Section 3.7.5 [Exit Status], page 38).