bash: Major Differences From The Bourne Shell
1
1 Appendix B Major Differences From The Bourne Shell
1 **************************************************
1
1 Bash implements essentially the same grammar, parameter and variable
1 expansion, redirection, and quoting as the Bourne Shell. Bash uses the
1 POSIX standard as the specification of how these features are to be
1 implemented. There are some differences between the traditional Bourne
1 shell and Bash; this section quickly details the differences of
1 significance. A number of these differences are explained in greater
1 depth in previous sections. This section uses the version of 'sh'
1 included in SVR4.2 (the last version of the historical Bourne shell) as
1 the baseline reference.
1
1 * Bash is POSIX-conformant, even where the POSIX specification
1 differs from traditional 'sh' behavior (⇒Bash POSIX Mode).
1
11 * Bash has multi-character invocation options (⇒Invoking
Bash).
1
1 * Bash has command-line editing (⇒Command Line Editing) and
1 the 'bind' builtin.
1
11 * Bash provides a programmable word completion mechanism (⇒
Programmable Completion), and builtin commands 'complete',
1 'compgen', and 'compopt', to manipulate it.
1
1 * Bash has command history (⇒Bash History Facilities) and the
1 'history' and 'fc' builtins to manipulate it. The Bash history
1 list maintains timestamp information and uses the value of the
1 'HISTTIMEFORMAT' variable to display it.
1
11 * Bash implements 'csh'-like history expansion (⇒History
Interaction).
1
1 * Bash has one-dimensional array variables (⇒Arrays), and the
1 appropriate variable expansions and assignment syntax to use them.
1 Several of the Bash builtins take options to act on arrays. Bash
1 provides a number of built-in array variables.
1
1 * The '$'...'' quoting syntax, which expands ANSI-C backslash-escaped
1 characters in the text between the single quotes, is supported
1 (⇒ANSI-C Quoting).
1
1 * Bash supports the '$"..."' quoting syntax to do locale-specific
1 translation of the characters between the double quotes. The '-D',
1 '--dump-strings', and '--dump-po-strings' invocation options list
11 the translatable strings found in a script (⇒Locale
Translation).
1
1 * Bash implements the '!' keyword to negate the return value of a
1 pipeline (⇒Pipelines). Very useful when an 'if' statement
1 needs to act only if a test fails. The Bash '-o pipefail' option
1 to 'set' will cause a pipeline to return a failure status if any
1 command fails.
1
11 * Bash has the 'time' reserved word and command timing (⇒
Pipelines). The display of the timing statistics may be
1 controlled with the 'TIMEFORMAT' variable.
1
1 * Bash implements the 'for (( EXPR1 ; EXPR2 ; EXPR3 ))' arithmetic
11 for command, similar to the C language (⇒Looping
Constructs).
1
1 * Bash includes the 'select' compound command, which allows the
1 generation of simple menus (⇒Conditional Constructs).
1
1 * Bash includes the '[[' compound command, which makes conditional
1 testing part of the shell grammar (⇒Conditional Constructs),
1 including optional regular expression matching.
1
1 * Bash provides optional case-insensitive matching for the 'case' and
1 '[[' constructs.
1
1 * Bash includes brace expansion (⇒Brace Expansion) and tilde
1 expansion (⇒Tilde Expansion).
1
1 * Bash implements command aliases and the 'alias' and 'unalias'
1 builtins (⇒Aliases).
1
DONTPRINTYET 11 * Bash provides shell arithmetic, the '((' compound command (⇒
Conditional Constructs), and arithmetic expansion (*noteShell
1DONTPRINTYET 11 * Bash provides shell arithmetic, the '((' compound command (⇒
Conditional Constructs), and arithmetic expansion (⇒Shell
Arithmetic).
1
1 * Variables present in the shell's initial environment are
1 automatically exported to child processes. The Bourne shell does
1 not normally do this unless the variables are explicitly marked
1 using the 'export' command.
1
1 * Bash supports the '+=' assignment operator, which appends to the
1 value of the variable named on the left hand side.
1
1 * Bash includes the POSIX pattern removal '%', '#', '%%' and '##'
1 expansions to remove leading or trailing substrings from variable
1 values (⇒Shell Parameter Expansion).
1
1 * The expansion '${#xx}', which returns the length of '${xx}', is
1 supported (⇒Shell Parameter Expansion).
1
1 * The expansion '${var:'OFFSET'[:'LENGTH']}', which expands to the
1 substring of 'var''s value of length LENGTH, beginning at OFFSET,
1 is present (⇒Shell Parameter Expansion).
1
1 * The expansion '${var/[/]'PATTERN'[/'REPLACEMENT']}', which matches
1 PATTERN and replaces it with REPLACEMENT in the value of 'var', is
1 available (⇒Shell Parameter Expansion).
1
1 * The expansion '${!PREFIX*}' expansion, which expands to the names
1 of all shell variables whose names begin with PREFIX, is available
1 (⇒Shell Parameter Expansion).
1
11 * Bash has INDIRECT variable expansion using '${!word}' (⇒Shell
Parameter Expansion).
1
1 * Bash can expand positional parameters beyond '$9' using '${NUM}'.
1
11 * The POSIX '$()' form of command substitution is implemented (⇒
Command Substitution), and preferred to the Bourne shell's '``'
1 (which is also implemented for backwards compatibility).
1
1 * Bash has process substitution (⇒Process Substitution).
1
1 * Bash automatically assigns variables that provide information about
1 the current user ('UID', 'EUID', and 'GROUPS'), the current host
1 ('HOSTTYPE', 'OSTYPE', 'MACHTYPE', and 'HOSTNAME'), and the
1 instance of Bash that is running ('BASH', 'BASH_VERSION', and
1 'BASH_VERSINFO'). ⇒Bash Variables, for details.
1
1 * The 'IFS' variable is used to split only the results of expansion,
1 not all words (⇒Word Splitting). This closes a longstanding
1 shell security hole.
1
1 * The filename expansion bracket expression code uses '!' and '^' to
1 negate the set of characters between the brackets. The Bourne
1 shell uses only '!'.
1
1 * Bash implements the full set of POSIX filename expansion operators,
1 including CHARACTER CLASSES, EQUIVALENCE CLASSES, and COLLATING
1 SYMBOLS (⇒Filename Expansion).
1
1 * Bash implements extended pattern matching features when the
1 'extglob' shell option is enabled (⇒Pattern Matching).
1
1 * It is possible to have a variable and a function with the same
1 name; 'sh' does not separate the two name spaces.
1
1 * Bash functions are permitted to have local variables using the
1 'local' builtin, and thus useful recursive functions may be written
1 (⇒Bash Builtins).
1
1 * Variable assignments preceding commands affect only that command,
1 even builtins and functions (⇒Environment). In 'sh', all
1 variable assignments preceding commands are global unless the
1 command is executed from the file system.
1
1 * Bash performs filename expansion on filenames specified as operands
1 to input and output redirection operators (⇒Redirections).
1
1 * Bash contains the '<>' redirection operator, allowing a file to be
1 opened for both reading and writing, and the '&>' redirection
1 operator, for directing standard output and standard error to the
1 same file (⇒Redirections).
1
1 * Bash includes the '<<<' redirection operator, allowing a string to
1 be used as the standard input to a command.
1
1 * Bash implements the '[n]<&WORD' and '[n]>&WORD' redirection
1 operators, which move one file descriptor to another.
1
1 * Bash treats a number of filenames specially when they are used in
1 redirection operators (⇒Redirections).
1
1 * Bash can open network connections to arbitrary machines and
1 services with the redirection operators (⇒Redirections).
1
1 * The 'noclobber' option is available to avoid overwriting existing
1 files with output redirection (⇒The Set Builtin). The '>|'
1 redirection operator may be used to override 'noclobber'.
1
1 * The Bash 'cd' and 'pwd' builtins (⇒Bourne Shell Builtins)
1 each take '-L' and '-P' options to switch between logical and
1 physical modes.
1
1 * Bash allows a function to override a builtin with the same name,
1 and provides access to that builtin's functionality within the
11 function via the 'builtin' and 'command' builtins (⇒Bash
Builtins).
1
1 * The 'command' builtin allows selective disabling of functions when
1 command lookup is performed (⇒Bash Builtins).
1
1 * Individual builtins may be enabled or disabled using the 'enable'
1 builtin (⇒Bash Builtins).
1
1 * The Bash 'exec' builtin takes additional options that allow users
1 to control the contents of the environment passed to the executed
1 command, and what the zeroth argument to the command is to be
1 (⇒Bourne Shell Builtins).
1
1 * Shell functions may be exported to children via the environment
1 using 'export -f' (⇒Shell Functions).
1
1 * The Bash 'export', 'readonly', and 'declare' builtins can take a
1 '-f' option to act on shell functions, a '-p' option to display
1 variables with various attributes set in a format that can be used
1 as shell input, a '-n' option to remove various variable
1 attributes, and 'name=value' arguments to set variable attributes
1 and values simultaneously.
1
1 * The Bash 'hash' builtin allows a name to be associated with an
1 arbitrary filename, even when that filename cannot be found by
11 searching the '$PATH', using 'hash -p' (⇒Bourne Shell
Builtins).
1
1 * Bash includes a 'help' builtin for quick reference to shell
1 facilities (⇒Bash Builtins).
1
1 * The 'printf' builtin is available to display formatted output
1 (⇒Bash Builtins).
1
1 * The Bash 'read' builtin (⇒Bash Builtins) will read a line
1 ending in '\' with the '-r' option, and will use the 'REPLY'
1 variable as a default if no non-option arguments are supplied. The
1 Bash 'read' builtin also accepts a prompt string with the '-p'
1 option and will use Readline to obtain the line when given the '-e'
1 option. The 'read' builtin also has additional options to control
1 input: the '-s' option will turn off echoing of input characters as
1 they are read, the '-t' option will allow 'read' to time out if
1 input does not arrive within a specified number of seconds, the
1 '-n' option will allow reading only a specified number of
1 characters rather than a full line, and the '-d' option will read
1 until a particular character rather than newline.
1
1 * The 'return' builtin may be used to abort execution of scripts
11 executed with the '.' or 'source' builtins (⇒Bourne Shell
Builtins).
1
1 * Bash includes the 'shopt' builtin, for finer control of shell
1 optional capabilities (⇒The Shopt Builtin), and allows these
11 options to be set and unset at shell invocation (⇒Invoking
Bash).
1
1 * Bash has much more optional behavior controllable with the 'set'
1 builtin (⇒The Set Builtin).
1
1 * The '-x' ('xtrace') option displays commands other than simple
11 commands when performing an execution trace (⇒The Set
Builtin).
1
1 * The 'test' builtin (⇒Bourne Shell Builtins) is slightly
1 different, as it implements the POSIX algorithm, which specifies
1 the behavior based on the number of arguments.
1
1 * Bash includes the 'caller' builtin, which displays the context of
1 any active subroutine call (a shell function or a script executed
1 with the '.' or 'source' builtins). This supports the bash
1 debugger.
1
1 * The 'trap' builtin (⇒Bourne Shell Builtins) allows a 'DEBUG'
1 pseudo-signal specification, similar to 'EXIT'. Commands specified
1 with a 'DEBUG' trap are executed before every simple command, 'for'
1 command, 'case' command, 'select' command, every arithmetic 'for'
1 command, and before the first command executes in a shell function.
1 The 'DEBUG' trap is not inherited by shell functions unless the
1 function has been given the 'trace' attribute or the 'functrace'
1 option has been enabled using the 'shopt' builtin. The 'extdebug'
1 shell option has additional effects on the 'DEBUG' trap.
1
1 The 'trap' builtin (⇒Bourne Shell Builtins) allows an 'ERR'
1 pseudo-signal specification, similar to 'EXIT' and 'DEBUG'.
1 Commands specified with an 'ERR' trap are executed after a simple
1 command fails, with a few exceptions. The 'ERR' trap is not
1 inherited by shell functions unless the '-o errtrace' option to the
1 'set' builtin is enabled.
1
1 The 'trap' builtin (⇒Bourne Shell Builtins) allows a
1 'RETURN' pseudo-signal specification, similar to 'EXIT' and
1 'DEBUG'. Commands specified with an 'RETURN' trap are executed
1 before execution resumes after a shell function or a shell script
1 executed with '.' or 'source' returns. The 'RETURN' trap is not
1 inherited by shell functions unless the function has been given the
1 'trace' attribute or the 'functrace' option has been enabled using
1 the 'shopt' builtin.
1
1 * The Bash 'type' builtin is more extensive and gives more
1 information about the names it finds (⇒Bash Builtins).
1
1 * The Bash 'umask' builtin permits a '-p' option to cause the output
1 to be displayed in the form of a 'umask' command that may be reused
1 as input (⇒Bourne Shell Builtins).
1
1 * Bash implements a 'csh'-like directory stack, and provides the
11 'pushd', 'popd', and 'dirs' builtins to manipulate it (⇒The
Directory Stack). Bash also makes the directory stack visible as
1 the value of the 'DIRSTACK' shell variable.
1
1 * Bash interprets special backslash-escaped characters in the prompt
1 strings when interactive (⇒Controlling the Prompt).
1
11 * The Bash restricted mode is more useful (⇒The Restricted
Shell); the SVR4.2 shell restricted mode is too limited.
1
1 * The 'disown' builtin can remove a job from the internal shell job
1 table (⇒Job Control Builtins) or suppress the sending of
1 'SIGHUP' to a job when the shell exits as the result of a 'SIGHUP'.
1
1 * Bash includes a number of features to support a separate debugger
1 for shell scripts.
1
1 * The SVR4.2 shell has two privilege-related builtins ('mldmode' and
1 'priv') not present in Bash.
1
1 * Bash does not have the 'stop' or 'newgrp' builtins.
1
1 * Bash does not use the 'SHACCT' variable or perform shell
1 accounting.
1
1 * The SVR4.2 'sh' uses a 'TIMEOUT' variable like Bash uses 'TMOUT'.
1
1 More features unique to Bash may be found in ⇒Bash Features.
1
1 B.1 Implementation Differences From The SVR4.2 Shell
1 ====================================================
1
1 Since Bash is a completely new implementation, it does not suffer from
1 many of the limitations of the SVR4.2 shell. For instance:
1
1 * Bash does not fork a subshell when redirecting into or out of a
1 shell control structure such as an 'if' or 'while' statement.
1
1 * Bash does not allow unbalanced quotes. The SVR4.2 shell will
1 silently insert a needed closing quote at 'EOF' under certain
1 circumstances. This can be the cause of some hard-to-find errors.
1
1 * The SVR4.2 shell uses a baroque memory management scheme based on
1 trapping 'SIGSEGV'. If the shell is started from a process with
1 'SIGSEGV' blocked (e.g., by using the 'system()' C library function
1 call), it misbehaves badly.
1
1 * In a questionable attempt at security, the SVR4.2 shell, when
1 invoked without the '-p' option, will alter its real and effective
1 UID and GID if they are less than some magic threshold value,
1 commonly 100. This can lead to unexpected results.
1
1 * The SVR4.2 shell does not allow users to trap 'SIGSEGV', 'SIGALRM',
1 or 'SIGCHLD'.
1
1 * The SVR4.2 shell does not allow the 'IFS', 'MAILCHECK', 'PATH',
1 'PS1', or 'PS2' variables to be unset.
1
1 * The SVR4.2 shell treats '^' as the undocumented equivalent of '|'.
1
1 * Bash allows multiple option arguments when it is invoked ('-x -v');
1 the SVR4.2 shell allows only one option argument ('-xv'). In fact,
1 some versions of the shell dump core if the second argument begins
1 with a '-'.
1
1 * The SVR4.2 shell exits a script if any builtin fails; Bash exits a
1 script only if one of the POSIX special builtins fails, and only
1 for certain failures, as enumerated in the POSIX standard.
1
1 * The SVR4.2 shell behaves differently when invoked as 'jsh' (it
1 turns on job control).
1