Chapter 1. The Bash Shell
This pocket reference covers Bash, particularly version 4.1, the primary shell for GNU/Linux and Mac OS X. Bash is available for Solaris and the various BSD systems, and can be easily compiled for just about any other Unix system. The following topics are covered:
Conventions
Filenames, command names, options and inline examples are shown in constant
width
. Input that a user should type in exactly as-is is shown in constant
width
userinput
. Items which should be replaced with real data in examples and syntax descriptions are shown in constant
width
replaceable
. New terms and emphasized items are shown in italics . Finally, references of the form name (N) refer to the manual page for name in section N of the online manual (accessed via the man command).
History
The original Bourne shell distributed with V7 Unix in 1979 became the standard shell for writing shell scripts. The Bourne shell is still found in /bin/sh
on many commercial Unix systems . It has not changed that much since its initial release, although it has seen modest enhancements over the years. The most notable new features added were the CDPATH
variable and a built-in test command with System III (circa 1980), command hashing and shell functions for System V Release 2 (circa 1984), and the addition of job control features for System V Release 4 (1989).
Because the Berkeley C shell ( csh ) offered features that were more pleasant for interactive use, such as command history and job control, for a long time the standard practice in the Unix world was to use the Bourne shell for programming and the C shell for daily use. David Korn at Bell Labs was the first developer to enhance the Bourne shell by adding csh -like features to it: history, job control, and additional programmability. Eventually, the Korn shells feature set surpassed both that of the Bourne and C shells, while remaining compatible with the former for shell programming. Today, the POSIX standard defines the standard shell language and behavior based on the System V Bourne shell, with a selected subset of features from the Korn shell.
The Free Software Foundation, in keeping with its goal to produce a complete Unix work-alike system, developed a clone of the Bourne shell, written from scratch, named Bash, the Bourne-Again SHell. Over time, Bash has become a POSIX-compliant version of the shell with many additional features overlapping those of the Korn shell, but Bash is not an exact Korn shell clone. Today, Bash is arguably the most widely used Bourne-derived shell.
Overview of Features
The Bash shell provides the following features:
Input/output redirection
Wildcard characters for filename abbreviation
Shell variables and options for customizing the environment
A built-in command set for writing shell programs
Shell functions, for modularizing tasks within a shell program
Job control
Command-line editing (using the command syntax of either vi or Emacs)
Access to previous commands (command history)
Integer arithmetic
Arrays and arithmetic expressions
Command-name abbreviation (aliasing)
Upwards compliance with POSIX
Internationalization facilities
An arithmetic for loop
Invoking the Shell
The command interpreter for the Bash shell ( bash ) can be invoked as follows:
bash [
options
] [
arguments
]
Bash can execute commands from a terminal, from a file (when the first argument is a script), or from standard input (if no arguments remain or if -s
is specified). The shell automatically prints prompts if standard input is a terminal, or if -i
is given on the command line.
On many systems, /bin/sh
is a link to Bash. When invoked as sh , Bash acts more like the traditional Bourne shell: login shells read /etc/profile
and ~/.profile
, and regular shells read $ENV
, if it is set. Full details are available in the bash (1) manpage .
Options
-c
str
Read commands from string str .
-D
,
--dump-strings
Print all $""
strings in the program.
-i
Create an interactive shell (prompt for input).
-l
,
--login
Shell is a login shell.
-O
option
Enable shopt option option . Use +O
to unset option .
-p
Start up as a privileged user. Do not read $ENV
or $BASH_ENV
; do not import functions from the environment; and ignore the values of the BASHOPTS
, CDPATH
, GLOBIGNORE
, and SHELLOPTS
variables. The normal fixed-name startup files (such as $HOME/.bash_profile
) are read.
-r
,
--restricted
Create a restricted shell.
-s
Read commands from standard input. Output from built-in commands goes to file descriptor 1; all other shell output goes to file descriptor 2.
--debugger
Read the debugging profile at startup and turn on the extdebug
option to shopt . For use by the Bash debugger (see http://bashdb.sourceforge.net).
--dump-po-strings
Same as -D
, but output in GNU