bash: Major Differences From The Bourne Shell

 
 Appendix B Major Differences From The Bourne Shell
 **************************************************
 
 Bash implements essentially the same grammar, parameter and variable
 expansion, redirection, and quoting as the Bourne Shell.  Bash uses the
 POSIX standard as the specification of how these features are to be
 implemented.  There are some differences between the traditional Bourne
 shell and Bash; this section quickly details the differences of
 significance.  A number of these differences are explained in greater
 depth in previous sections.  This section uses the version of 'sh'
 included in SVR4.2 (the last version of the historical Bourne shell) as
 the baseline reference.
 
    * Bash is POSIX-conformant, even where the POSIX specification
      differs from traditional 'sh' behavior (SeeBash POSIX Mode).
 
    * Bash has multi-character invocation options (SeeInvoking
      Bash).
 
    * Bash has command-line editing (SeeCommand Line Editing) and
      the 'bind' builtin.
 
    * Bash provides a programmable word completion mechanism (See
      Programmable Completion), and builtin commands 'complete',
      'compgen', and 'compopt', to manipulate it.
 
    * Bash has command history (SeeBash History Facilities) and the
      'history' and 'fc' builtins to manipulate it.  The Bash history
      list maintains timestamp information and uses the value of the
      'HISTTIMEFORMAT' variable to display it.
 
    * Bash implements 'csh'-like history expansion (SeeHistory
      Interaction).
 
    * Bash has one-dimensional array variables (SeeArrays), and the
      appropriate variable expansions and assignment syntax to use them.
      Several of the Bash builtins take options to act on arrays.  Bash
      provides a number of built-in array variables.
 
    * The '$'...'' quoting syntax, which expands ANSI-C backslash-escaped
      characters in the text between the single quotes, is supported
      (SeeANSI-C Quoting).
 
    * Bash supports the '$"..."' quoting syntax to do locale-specific
      translation of the characters between the double quotes.  The '-D',
      '--dump-strings', and '--dump-po-strings' invocation options list
      the translatable strings found in a script (SeeLocale
      Translation).
 
    * Bash implements the '!' keyword to negate the return value of a
      pipeline (SeePipelines).  Very useful when an 'if' statement
      needs to act only if a test fails.  The Bash '-o pipefail' option
      to 'set' will cause a pipeline to return a failure status if any
      command fails.
 
    * Bash has the 'time' reserved word and command timing (See
      Pipelines).  The display of the timing statistics may be
      controlled with the 'TIMEFORMAT' variable.
 
    * Bash implements the 'for (( EXPR1 ; EXPR2 ; EXPR3 ))' arithmetic
      for command, similar to the C language (SeeLooping
      Constructs).
 
    * Bash includes the 'select' compound command, which allows the
      generation of simple menus (SeeConditional Constructs).
 
    * Bash includes the '[[' compound command, which makes conditional
      testing part of the shell grammar (SeeConditional Constructs),
      including optional regular expression matching.
 
    * Bash provides optional case-insensitive matching for the 'case' and
      '[[' constructs.
 
    * Bash includes brace expansion (SeeBrace Expansion) and tilde
      expansion (SeeTilde Expansion).
 
    * Bash implements command aliases and the 'alias' and 'unalias'
      builtins (SeeAliases).
 
DONTPRINTYET     * Bash provides shell arithmetic, the '((' compound command (See
      Conditional Constructs), and arithmetic expansion (*noteShell
DONTPRINTYET     * Bash provides shell arithmetic, the '((' compound command (See
      Conditional Constructs), and arithmetic expansion (SeeShell

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