Exploring the latest version of the great Bourne-again shell
A Good Bash
© yewkeo, 123RF
Despite the Bourne-again shell's biblical age and high level of maturity, developers continue to work on it. We take a look at the latest Bash release.
Despite recent competition from powerful alternatives such as Zsh [1], the Bourne-again shell (Bash) [2] is still the king of the hill on the Linux console. Users can use Bash interactively, and it also serves as a simple yet practical scripting language. Bash is part of the backbone of any working Linux system – all the more reason to investigate the benefits of upgrading to the new Bash 4 release, which appeared February 2009.
Why or Why Not?
On production systems, you might want to consider whether it is really necessary to upgrade to Bash 4. The major distributions will eventually spread the new version through their own updates, so the new Bash will reach you someday whether you download and install it or not. Programmers and many power users, on the other hand, like to embrace the goodies a new version offers as quickly as possible.
If you want to get a head start on new Bash features that are making their way to the next generation of Linux systems, you'll enjoy spending some quality time with Bash 4.
Table 1 provides a summary of some important new features; for a complete list, check out the NEWS file in the Bash documentation. Here, we highlight some of the most important changes.
At the Command Line
Command-line users will appreciate a few inconspicuous, but very useful, extensions debuting with the latest Bash. For example, the string expands to a list of files and paths below the current working directory in a fashion similar to the external find command. However, users need to enable the feature by issuing the command shopt -s globstar.
The developers have now adopted a more user friendly approach to what used to be one of the greatest mysteries of the Bourne shell: the way standard error message output is redirected. Instead of the 2>&1 1>file mantra, users can now use &>> file to redirect both error and standard output into a file. The |& shortcut, which redirects the standard error for a command to a pipe, is another useful addition.
Associative Arrays
One popular myth is that Bash scripts create too many processes, which ultimately affects performance. But many of the more simple applications once used with Bash, including sed, grep, basename, or dirname are no longer necessary; Bash handles these tasks just as quickly as any other scripting language with on-board tools. Despite these improvements, Bash programmers cast many an envious glance at Perl and Python, both of which have more versatile data structures.
In Version 4, Bash finally adds associative arrays to its existing single-dimensional arrays. For many coders, this change in itself is reason enough to move to the new version because it provides a more elegant approach to many problems. For example, developers can use arbitrary strings as indices in associative arrays, rather than just as integers. Listing 1 gives an example.
Listing 1
Programming with Associative Arrays
01 #!/bin/bash
02
03 declare -A name
04
05 name["Linus"]="Torvalds"
06 name["Bill"]="Gates"
07 name["Steve"]="Jobs"
08 name["George W."]="Bush"
09
10 # output all values:
11 echo "Values: ${name[@]}"
12
13 # output all keys:
14 echo "Key: ${!name[@]}"
15
16 # Access individual values
17 for v in "${!name[@]}"; do
18 echo "$v ${name[$v]}"
19 done
Listing 2 is a script that sorts files in directories according to their properties – for example, the creation date. The script has two fundamental problems: First, it does not work with directories that contain blanks, and second, it might process individual directories multiple times.
Listing 2
Erroneous Strings in Lists
01 #!/bin/bash 02 03 dirs="" 04 05 for f in "$@"; do 06 d=`getDir "$f"` 07 mkdir -p "$d" 08 mv -f "$f" "$d" 09 dirs="$dirs $d" 10 done 11 12 for d in $dirs; do 13 createIndex "$d" 14 done
Previous Bash versions have taken different approaches to solving this issue. In Bash 3.2, programmers can store quoted directory names in strings or an array. A script could prevent double processing by searching in the string – or by a slow linear search in the array. Neither of these approaches is particularly elegant.
Bash 4 handles this task far more simply (see Listing 3). The directory name serves as a key; the value itself is of no interest. As of line 12, the loop iterates over all the keys. The special construction with ampersands in double quotes does what it does anywhere in Bash: It tells the shell to process the values as individual tokens. The solution thus works with blanks in the target directories.
Listing 3
Associative Arrays
01 #!/bin/bash
02
03 declare -A dirs
04
05 for f in "$@"; do
06 d=`getDir "$f"`
07 mkdir -p "$d"
08 mv -f "$f" "$d"
09 dirs[$d]=1
10 done
11
12 for d in "${!dirs[@]}"; do
13 createIndex "$d"
14 done
Our Services
Direct Download
Tag Cloud
News
-
Google and NASA Partner in Quantum Computing Project
Vendor D-Wave scores big with a sale to NASA's Quantum Intelligence Lab.
-
Mageia Project Announces Mageia 3 Linux
Many package updates and Steam integration highlight the latest from the Mandriva-based community Linux.
-
FSF Outs the World Wide Web Consortium over DRM Proposal
Richard Stallman calls for the W3C to remain independent of vendor interests.
-
Debian 7.0 Debuts
The new release supports nine architectures, 73 human languages, and zero non-Free components.
-
Alpha Version of Fedora 19 Released
Fedora developers release the first alpha version of Fedora 19, known as Schrödinger’s Cat, for general testing. The final release is expected in July 2013.
-
ack 2.0 Released
ack is a grep-like, command-line tool that has been optimized for programmers to search large trees of source code.
-
SUSE Studio 1.3 Released
New features in SUSE Studio 1.3 include enhanced cloud integration, VM platform support, and lifecycle management.
-
Xen To Become Linux Foundation Collaborative Project
The Linux Foundation recently announced that the Xen Project is becoming a Linux Foundation Collaborative Project.
-
RunRev Releases Open Source Version of LiveCode
Open source version of LiveCode is now available for developing apps, games, and utilities for all major platforms.
-
OpenDaylight Project Formed
OpenDaylight is an open source software-defined networking project committed to furthering adoption of SDN and accelerating innovation in a vendor-neutral and open environment.
