Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev | Chapter 34. Bash, versions 2 and 3 | Next |
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.
1 #!/bin/bash 2 3 for i in {1..10} 4 # Simpler and more straightforward than 5 #+ for i in $(seq 10) 6 do 7 echo -n "$i " 8 done 9 10 echo 11 12 # 1 2 3 4 5 6 7 8 9 10 |
The ${!array[@]} operator, which expands to all the indices of a given array.
1 #!/bin/bash 2 3 Array=(element-zero element-one element-two element-three) 4 5 echo ${Array[0]} # element-zero 6 # First element of array. 7 8 echo ${!Array[@]} # 0 1 2 3 9 # All the indices of Array. 10 11 for i in ${!Array[@]} 12 do 13 echo ${Array[i]} # element-zero 14 # element-one 15 # element-two 16 # element-three 17 # 18 # All the elements in Array. 19 done |
The =~ Regular Expression matching operator within a double brackets test expression. (Perl has a similar operator.)
1 #!/bin/bash 2 3 variable="This is a fine mess." 4 5 echo "$variable" 6 7 if [[ "$variable" =~ "T*fin*es*" ]] 8 # Regex matching with =~ operator within [[ double brackets ]]. 9 then 10 echo "match found" 11 # match found 12 fi |
Or, more usefully:
1 #!/bin/bash 2 3 input=$1 4 5 6 if [[ "$input" =~ "[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] 7 # NNN-NN-NNNN 8 # Where each N is a digit. 9 # But, initial digit must not be 0. 10 then 11 echo "Social Security number." 12 # Process SSN. 13 else 14 echo "Not a Social Security number!" 15 # Or, ask for corrected input. 16 fi |
For another example of using the =~ operator, see Example A-28.
![]() | 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). |