Writing clearer Bash scripts

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Natalia Klenova, 123RF

Natalia Klenova, 123RF

Right Ingredients

Does the ingenious script your wrote last month look like confusing spaghetti code today? We show some proven style guidelines that can provide order and readability.

Bash scripts help rename files, sort a large address list, or execute other similarly tedious tasks. Scripts created under pressure can often look garbled (see Listing 1 for a prime example). Quick hacks may have their place, but they are often confusing and might be totally unclear a week later. For this reason, many users don't share their scripts, feeling they are not up to par. All this contributes to making such scripts hard to expand or change down the road.

Listing 1

Unformatted Script

a="100"; b="3"; c="."; d="shot"; for i in `seq 1 $a`; \
  do import -window root $c/$d$i.png; sleep $b; done; exit


For these reasons it's best to structure, format and, above all, comment your Bash scripts from the outset. Some guidelines can be helpful, such as the one from Google [1] or the handy PDF booklet written by Fritz Mehner [2] that you can print and keep by you computer (Figures 1 and 2). Whether you choose a commercial style guide or one from academia, the basic guidelines are the same.

Figure 1: You get details on individual guidelines at Google by clicking the triangle in front of a guideline.
Figure 2: Fritz Mehner's booklet is handy, 13-page manual you can keep by your computer for reference.

Your script should always identify the shell it is using at the beginning. If the script uses Bash functions, include #!/bin/bash on the first line. That way, systems that don't use Bash as the default shell, but have it installed, can still use it.

Fresh Air

To make the script more readable, get used to using the proper indentation and including blank lines. The confusing elements in Listing 1 become less cryptic if presented as in Listing 2.

Listing 2

Script with Proper Indentation

for i in `seq 1 $a`
  import -window root $c/$d$i.png;
  sleep $b

Each line has a separate instruction and a blank line separates blocks of related code, such as the block for the for-do loop. The statements in a block should be indented, such as the do portion of the loop. If you use space characters or a tab for indentation, different editors might interpret them differently. Style guides almost unanimously suggest using a double space character in place of a tab.

Notice that the do command is by itself on a line. Google, however, suggests including it on the previous for line. The same goes for while loops and the then in an if block. The else, however, is often best placed on its separate line, as is the ending done. As for if statements, Google recommends replacing test with double square brackets as abbreviations:

if [[ ${filename} == "image*" ]]; then ...

This notation has the advantage of allowing for the use of regular expressions. It also reduces errors in that the double square brackets neither arbitrarily truncate words nor expand filenames.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content