Bash

Introduction

What is Bash?

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the "Bourne-Again SHell", a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh.

What is a shell?

A shell is simply a macro processor that executes commands. The term "macro processor" means functionality where text and symbols are expanded to create larger expressions.

A Unix shell is both a command interpreter and a programming language:

Shells may be used interactively or non-interactively:

A shell allows execution of GNU commands, both synchronously and asynchronously:

Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example:

While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.

Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases.

Definitions

Relationships of some definitions *

Basic Shell Features

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 structures, 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.

Shell Syntax

[s3.1]

Shell Operation

Basically, the shell does the following:

  1. Reads its input in one of the following way:
    • from a file (see Shell Scripts),
    • from a string supplied as an argument to the -c invocation option (see Invoking Bash),
    • from the user’s terminal.
  2. Breaks the input into words and operators, obeying the quoting rules described in Quoting. These tokens are separated by metacharacters. Alias expansion is performed by this step (see Aliases).
  3. Parses the tokens into simple and compound commands (see Shell Commands).
  4. Performs the various shell expansions (see Shell Expansions), breaking the expanded tokens into lists of filenames (see Filename Expansion) and commands and arguments.
  5. Performs any necessary redirections (see Redirections) and removes the redirection operators and their operands from the argument list.
  6. Executes the command (see Executing Commands).
  7. Optionally waits for the command to complete and collects its exit status (see Exit Status).
Quoting

Quoting is used to remove the special meaning of certain characters or words to the shell. It is used to:

There are three quoting mechanisms: the escape character, single quotes, and double quotes.

Escape Character

A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation.

Single Quotes

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Double Quotes

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’.

Examples on history expansion and ‘!’ (history expansion enabled):

$ foo
-bash: foo: command not found
$ echo "!!"
echo "foo"
foo
$ echo "\!!"
\!!

References