| ★ wanayoo — archive 1999 http://www.computerbits.com/archive/19971100/lnx9711.htm | Nouvelle recherche | Portail wanayoo |
![]() |
![]() |
| November 1997 | Volume 7 Number 11 |
A survey of Unix shells ... by Terry Griffin
What is a shell program? It's a text-based, general-purpose computer program that reads and processes commands from the operator as entered at the keyboard. This definition has been under assault of late with the so called "graphical shells" but for the purposes of this month's column we'll stick to the old text-based restriction.
In the DOS world shells are called "command processors." In DOS and Windows 95 the default command processor is COMMAND.COM; in DOS' descendents (OS/2 and Windows NT) it's CMD.EXE.
In both Unix and DOS you get to choose which shell or command processor you want to use. With DOS, people have not historically taken advantage of this option despite the availability of products such as 4DOS. Things are different in Unix world. There are several popular shell programs, and everybody has his or her personal favorite. This month we take a quick tour of the most popular shells.
Shells serve two purposes. First, they process commands interactively one at a time as entered by the operator. Second, they execute sequences of commands from a text file. In DOS these are called "batch files," which are recognized by the .BAT filename extension. In Unix they are called "shell scripts" and usually they have no file name extension, but they can be identified with the file command -- or by examining the first line of the file, as we'll see later.
While many shell features apply to both interactive and script modes, some features exist to support just one mode or the other. For example command history, command line editing, and filename completion make sense only in interactive mode, while control structures (if-then-else and looping) are provided primarily for use in scripts.
One of the results of this dual nature of shells is that it's not uncommon for someone to have two favorite shells, one for interactive use and another for script programming.
The Bourne Shell, although not the first Unix shell program, is the grandfather of all modern Unix shells. It was written by Steve Bourne at AT&T and is installed as /bin/sh. This is the only shell guaranteed to be on any Unix system you might encounter. In many cases, however, you'll find that /bin/sh is not a real Bourne shell. Instead it is a symbolic link to a more modern shell that has backward compatibility with the Bourne shell.
You won't find the Bourne shell being used much interactively these days -- it doesn't contain any of the fancy interactive features of newer shells -- but it remains immensely popular for scripts for two reasons: it's a pretty fair scripting language, but more importantly it's available on every Unix box a script might find itself being executed. Where Bourne shell falls short scripting-wise, other widely available utilities such as the AWK language interpreter (awk) and the stream editor (sed) are used within Bourne shell scripts.
The Bourne Again Shell (bash) is a product Free Software Foundation's GNU project. It is backward compatible with Bourne shell and contains all of the nicer features of both csh and ksh, and like ksh93 it meets the POSIX 1003.2 standard.
This is the default Linux shell and is usually installed as /bin/bash with a symbolic link to /bin/sh. On commercial Unix systems you may find that someone has installed it as /usr/local/bin/bash. If not, you can always get the source code from the GNU software archives and install it yourself.
Command line history and editing with bash are similar to what you get with the DOSKEY utility under DOS, with the CMD.EXE programs in DOS descendants, or with DCL on VAX/VMS systems. Up and down arrows scroll you through the history buffer and left and right arrows are used for command editing.
The C Shell (csh) was written by William Joy at Berkeley and, in retrospect, is widely regarded as a mistake. The idea was to have a programmer's shell with a syntax similar to the C programming language. (It seemed like a good idea at the time.) Despite the C language heritage, csh proved to be unsuitable for high-powered script programming, but it did gain popularity as an interactive shell because it pioneered command history. The C Shell is usually installed as /bin/csh.
Command history under C Shell differs from other shells in that there's no mechanism to scroll through the history buffer. Previous commands can only be referenced by a unique event number assigned to each command. Constructing new commands from old commands is done with sed-like editing commands.
A later effort, also involving William Joy, improved on C Shell by adding command line editing. The result was the TC Shell (tcsh). You can configure the editing for vi-like or emacs-like modes. TC Shell is usually installed as /bin/tcsh and sometimes symbolically linked to /bin/csh.
Unlike most of the other shells discussed here csh and tcsh are not backward compatible with the Bourne Shell.
You have to be careful with the words "POSIX shell." They can mean one of two things. The POSIX shell typically refers to a Bourne shell which has been updated to account for capabilities required by the POSIX 1003.2 standard. A POSIX shell refers to any shell program which meets this standard.
The POSIX shell has come to resemble ksh (described below). It is typically installed as /bin/sh or /bin/posix/sh.
The Korn Shell (ksh), a product of AT&T, was a successful attempt to provide the functionality of C Shell while using a Bourne Shell syntax and maintaining Bourne Shell backward compatibility.
There have been two incarnations thus far, ksh88 and ksh93. The later satisfies the POSIX 1003.2 standard and improves on the scripting capabilities. A genuine AT&T Korn Shell is not normally distributed with Linux, but ksh93 binaries for Linux-x86-ELF and several other Unixes are available at no charge for non-commercial use. Download the ast-base-97 package which includes ksh93 among other things.
The Public Domain Korn Shell (pdksh) was written by lots of people and is currently maintained by Michael Rendell. It is a very good clone of ksh93 and also borrows a few ideas from bash (below).
It is included with most Linux distributions as /bin/ksh and perhaps also as /usr/bin/ksh and /usr/bin/pdksh. On commercial Unix systems you may find it as /usr/local/bin/ksh or /usr/local/bin/pdksh. If you should find yourself without a Korn Shell or stuck with the older ksh88 you can build and install pdksh yourself from the source code.
As it can with tcsh, command editing under Korn shells can be configured for vi-mode or emacs-mode. There is both a csh-style command history feature and a scrollable command history using the vi-mode or emacs-mode keys.
A Shell (ash) by Kenneth Almquist of Berkely is a lightweight Bourne Shell clone which you may find suitable for use on machines that are very tight on memory. It's usually installed as /bin/ash and it may also have symbolic links to /bin/bsh and /bin/sh.
The Z Shell (zsh) by Paul Falstad resembles the Korn Shell in many respects but has some extra features, including built-in spell checking of all things. It's usually installed as /bin/zsh.
Both ash and zsh are included with most Linux distributions.
Your login shell is determined by the seventh field of your entry in the /etc/passwd file. If this field is blank it defaults to /bin/sh. You can change your login shell with the chsh command. On many Unix systems your choice of login shells will be restricted to those listed in the /etc/shells file.
Once you are logged in, the SHELL environment variable determines which shell is started whenever you need a new shell process, such as when you open a new terminal window.
In shell script files, if the first two characters of the file are #! then the name of the program following the exclamation point is the program which is supposed to process the script. If this line is not present then the shell invoking the script will attempt to process it. For example, if the first line of a script is #!/bin/csh then the script will be processed by the C Shell no matter which shell invoked it. This doesn't apply just to shells. For example if the first line of a file is #!/bin/more then this file, when executed, would display itself with the more command.
Shells are a very personal things and as such it's difficult to say with any objective certainty that one shell is superior to all the others. Debates rage on, but I find this about as useful as arguments about pizza toppings. That said, I give you the following guidelines.
In case you're wondering, I use the Korn Shell as a result of starting out on HP-UX, Hewlett-Packard's Unix OS. I tried switching to bash a couple of times, but my fingers refused to go along. Despite my continued use of ksh, I think bash is the best shell available today.
The shell FAQs is worthwhile reading, as are the discussions of shells found in comp.unix.shell.
You can also get a listing of articles by Terry Griffin
If you need instructions for our search engine, go to our main search page.
| This article was originally published in the November 1997 issue of Computer Bits magazine, and is copyright © 1997 by Bitwise Productions, Inc., Forest Grove, OR, (503) 359-9107. All rights reserved. Archival material is provided as-is. Links are not necessarily maintained. Recent events compel us, sadly, to emphasize that your rights to this article are limited to viewing it and printing it for personal use only. You must receive explicit permission from Computer Bits and the author(s) before reprinting or redistributing this article in any medium. |