DistributedNetworks DistributedNetworks


Shell Programming  «Prev  Next»
Lesson 3 Deciding which shell to program for
Objective Distinguish between the primary uses of each shell to decide which to use.

Deciding on which shell to utilize for your Programming Tasks

Default shells

Distinguish between the primary uses of each shell to decide which to use. With all of the shells available on UNIX, the natural question is,
Which shell should I use?

Of course, this question has probably already been answered by your system administrator. Each user account on UNIX has a default shell assigned to it. This shell is the working environment you see each time you start a command-line session. You can ask your system administrator to change your default shell if you like; most UNIX systems include several shells. To see which shell you are using, enter the command ps and look for one of the shell names in the brief list of processes that appears. (The name will be something like sh, bash, ksh, csh, or tcsh.)

Shells for interactive work

All of the popular shells today (Korn, TENEX C shell, bash) include features that make them easy to use as you work at a command line.These features include:
  1. Command history, allowing you to re-use a previous command without typing it again
  2. Aliases (string substitutions) for your favorite commands
  3. Filename completion, allowing you to press a key (usually Tab) to fill in a long or complex filename
  4. Built-in arithmetic functions, so you can complete math calculations at the command line
  5. Ability to manage multiple jobs (processes) from a single command line

Choosing a programming shell

For this course, however, the more important question is this: which shell should you program for? Because the C shell was written to focus on interactive use rather than shell programming, it is generally considered to be a poor choice for shell scripts. The tcsh and zsh shells, both based on the same programming syntax as the C shell, correct some of the faults of the C shell, but still are not popular for shell programming.
The weaknesses of the C shell include:
  1. No strong input and output controls
  2. No functions (sub-routines)
  3. Ambiguous syntax leading to strange errors or unexpected results
This leads us back to the shells that use the original Bourne shell programming model.
So, which do we use?
Fortunately, programs written for these shells are compatible with each other (for the most part).
This course focuses on scripts for the Bourne shell (sh). Several reasons support this focus:
  1. The sh shell is available on virtually all UNIX systems
  2. A script written for the sh shell will likely run on bash or any Korn shell as well.
  3. Many of the scripts you will see as you work in UNIX are written for the sh shell
  4. The sh shell provides a lowest common denominator, rather than relying on special features in a newer, less well-known shell.
As scripts are introduced in this course, notes will indicate if a feature is not supported by one of the other popular shells. Tips also provide guidance for those who decide to write C shell scripts.

Few programming enhancements have been made since the original Bourne sh shell was developed. By programming for the sh shell, you gain compatibility without giving up fancy new features (there are not any to speak of).
The UNIX account that you have been given for this course, referred to here as the UNIX labs, is set up with the C shell as the default command-line environment. However, this does not prevent you from using the Bourne shell for your scripts, as you willl learn in a later module.
Shell scripts are used throughout your UNIX system. The next lesson explains the details.

Unix Shell - Quiz

Click the Quiz link below to test what you have learned so far about shells.
Unix Shell - Quiz