Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

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.
    #!/bin/bash
    
    for i in {1..10}
    #  Simpler and more straightforward than
    #+ for i in $(seq 10)
    do
      echo -n "$i "
    done
    
    echo
    
    # 1 2 3 4 5 6 7 8 9 10

  • The ${!array[@]} operator, which expands to all the indices of a given array.
    #!/bin/bash
    
    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[@]}
    do
      echo ${Array[i]} # element-zero
                       # element-one
                       # element-two
                       # element-three
                       #
                       # All the elements in Array.
    done

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

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

    Or, more usefully:

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

    For another example of using the =~ operator, see Example A-28.

Caution

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).

 
 
  Published under the terms of the GNU General Public License Design by Interspire