Home Page Home Page
 Home | Software Development | System/Network Administration | Corporate Services | Resources | About Us
Monthly Server Management One-time Server Services Other Services
Network Administration Network Monitoring Network Security High Availability Load Balancing Data Backup and Recovery
Linux HOWTOs Linux Guides New RFCs Vulnerability list
Partners Careers Site Map
Bash, version 3

34.2. Bash, version 3

On July 27, 2004, Chet Ramey released version 3 of Bash. This update fixes quite a number of bug in Bash and adds some new features.

Some of the added features are:

  • A new, more generalized {a..z} brace expansion operator.

    for i in {1..10}
    #  Simpler and more straightforward than
    #+ for i in $(seq 10)
      echo -n "$i "
    # 1 2 3 4 5 6 7 8 9 10
    # Or just . . .
    echo {a..z}    #  a b c d e f g h i j k l m n o p q r s t u v w x y z
    echo {z..a}    #  z y x w v u t s r q p o n m l k j i h g f e d c b a
                   #  Works backwards, too.
    echo {3..-2}   #  3 2 1 0 -1 -2
    echo {X..d}    #  X Y Z [  ] ^ _ ` a b c d
                   #  Shows (some of) the the ASCII characters between Z and a,
                   #+ but don't rely on this type of behavior because . . .
    echo {]..a}    #  {]..a}
                   #  Why?

  • The ${!array[@]} operator, which expands to all the indices of a given array.

    Array=(element-zero element-one element-two element-three)
    echo ${Array[0]}   # element-zero
                       # First element of array.
    echo ${!Array[@]}  # 0 1 2 3
                       # All the indices of Array.
    for i in ${!Array[@]}
      echo ${Array[i]} # element-zero
                       # element-one
                       # element-two
                       # element-three
                       # All the elements in Array.

  • The =~ Regular Expression matching operator within a double brackets test expression. (Perl has a similar operator.)

    variable="This is a fine mess."
    echo "$variable"
    if [[ "$variable" =~ "T*fin*es*" ]]
    # Regex matching with =~ operator within [[ double brackets ]].
      echo "match found"
          # match found

    Or, more usefully:

    if [[ "$input" =~ "[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
    # Where each N is a digit.
    # But, initial digit must not be 0.
      echo "Social Security number."
      # Process SSN.
      echo "Not a Social Security number!"
      # Or, ask for corrected input.

    For additional examples of using the =~ operator, see Example A-29 and Example 18-14.

  • The new set -o pipefail option is useful for debugging pipes. If this option is set, then the exit status of a pipe is the exit status of the last command in the pipe to fail (return a non-zero value), rather than the actual final command in the pipe.

    See Example 15-39.


The update to version 3 of Bash breaks a few scripts that worked under earlier versions. Test critical legacy scripts to make sure they still work!

As it happens, a couple of the scripts in the Advanced Bash Scripting Guide had to be fixed up (see Example A-20 and Example 9-4, for instance).

34.2.1. Bash, version 3.1

The version 3.1 update of Bash introduces a number of bugfixes and a few minor changes.

  • The += operator is now permitted in in places where previously only the = assignment operator was recognized.

    echo $a        # 1
    a+=5           # Won't work under versions of Bash earlier than 3.1.
    echo $a        # 15
    echo $a        # 15Hello

    Here, += functions as a string concatenation operator. Note that its behavior in this particular context is different than within a let construct.

    echo $a        # 1
    let a+=5       # Integer arithmetic, rather than string concatenation.
    echo $a        # 6
    let a+=Hello   # Doesn't "add" anything to a.
    echo $a        # 6