36.9. Portability Issues

 

It is easier to port a shell than a shell script.

--Larry Wall

This book deals specifically with Bash scripting on a GNU/Linux system. All the same, users of sh and ksh will find much of value here.

As it happens, many of the various shells and scripting languages seem to be converging toward the POSIX 1003.2 standard. Invoking Bash with the --posix option or inserting a set -o posix at the head of a script causes Bash to conform very closely to this standard. Another alternative is to use a #!/bin/sh sha-bang header in the script, rather than #!/bin/bash. [1] Note that /bin/sh is a link to /bin/bash in Linux and certain other flavors of UNIX, and a script invoked this way disables extended Bash functionality.

Most Bash scripts will run as-is under ksh, and vice-versa, since Chet Ramey has been busily porting ksh features to the latest versions of Bash.

On a commercial UNIX machine, scripts using GNU-specific features of standard commands may not work. This has become less of a problem in the last few years, as the GNU utilities have pretty much displaced their proprietary counterparts even on "big-iron" UNIX. Caldera's release of the source to many of the original UNIX utilities has accelerated the trend.

Bash has certain features that the traditional Bourne shell lacks. Among these are:

See the Bash F.A.Q. for a complete listing.

36.9.1. A Test Suite

Let us illustrate some of the incompatibilities between Bash and the classic Bourne shell. Download and install the "Heirloom Bourne Shell" and run the following script, first using Bash, then the classic sh.


Example 36-23. Test Suite

   1 #!/bin/bash
   2 # test-suite.sh
   3 # A partial Bash compatibility test suite.
   4 # Run this on your version of Bash, or some other shell.
   5 
   6 default_option=FAIL         # Tests below will fail unless . . .
   7 
   8 echo
   9 echo -n "Testing "
  10 sleep 1; echo -n ". "
  11 sleep 1; echo -n ". "
  12 sleep 1; echo ". "
  13 echo
  14 
  15 # Double brackets
  16 String="Double brackets supported?"
  17 echo -n "Double brackets test: "
  18 if [[ "$String" = "Double brackets supported?" ]]
  19 then
  20   echo "PASS"
  21 else
  22   echo "FAIL"
  23 fi
  24 
  25 
  26 # Double brackets and regex matching
  27 String="Regex matching supported?"
  28 echo -n "Regex matching: "
  29 if [[ "$String" =~ R.....matching* ]]
  30 then
  31   echo "PASS"
  32 else
  33   echo "FAIL"
  34 fi
  35 
  36 
  37 # Arrays
  38 test_arr=$default_option     # FAIL
  39 Array=( If supports arrays will print PASS )
  40 test_arr=${Array[5]}
  41 echo "Array test: $test_arr"
  42 
  43 
  44 # Command Substitution
  45 csub_test ()
  46 {
  47   echo "PASS"
  48 }
  49 
  50 test_csub=$default_option    # FAIL
  51 test_csub=$(csub_test)
  52 echo "Command substitution test: $test_csub"
  53 
  54 echo
  55 
  56 #  Completing this script is an exercise for the reader.
  57 #  Add to the above similar tests for double parentheses,
  58 #+ brace expansion, process substitution, etc.
  59 
  60 exit $?

Notes

[1]

Or, better yet, #!/bin/env sh.