What Shell

Many shells are available for Linux. When writing a shell script, you should be aware of this and choose the best shell available for your script. A script written for one shell will not necessarily run on another shell well. Fortunately, the choice is relatively easy because Bash (/bin/bash) is the default shell on Linux. Bash by itself is compatible with the Unix Bourne shell (/bin/sh), which has been used on Unix since the 1970s. The good part of this compatibility is that a script that was written to run on /bin/sh will work on Bash. The opposite is, however, not necessarily true. This is because many new features have been added to Bash that don't exist in the traditional Unix Bourne shell.

In addition to Bash, you will occasionally encounter other shells on Linux. The most important of these is the Korn shell (/bin/ksh), which is the default shell on the Sun Microsystems Solaris operating system. An open source derivative of that shell is available as the Public Domain Korn Shell (/bin/pdksh). Another popular shell is the C shell, which on Linux exists as /bin/tcsh. The C shell is especially popular amongst C programmers; this is because the C shell has a scripting language that resembles the C programming language. You will sometimes encounter C-shell users in a Linux environment. The Korn shell, however, is not used often in Linux environments. The reason is that almost all its important features are offered by Bash as well.

Both the Korn shell and the C shell are not compatible with Bash (although a really simple script will run in all shells). As a result, you will not be able to run a C-shell script in a Bash environment. This has a solution, though—just include the shebang in your shell script. This is an indicator of the program that must be used when executing the script. The shebang is a hash, followed by an exclamation mark, which is followed by the name of the command interpreter. If the binary referred to by the shebang is present on your system, the script can run anyway, no matter what shell environment you are currently in as a user. Listing 27-1 shows an example of a script that starts with a shebang.

Listing 27-1. Shell Scripts Should Always Start with the Shebang

# myscript [filename]

Was this article helpful?

0 0

Post a comment