The shell is a special program used as an interface between the user and the heart of the operating system, a program called the kernel,The kernel is loaded into memory at boot time, and manages the system until shutdown. It creates and controls processes, manages memory, file systems, communications, and so forth. All other programs, including shell programs, reside on the disk. The kernel loads programs from the disk into memory, executes them, and cleans up the system when they terminate. The shell is a utility program that starts up when you log on. It allows users to interact with the kernel by interpreting commands that are typed either at the command line or in a script file.
When you log on, an interactive shell starts up and prompts you for input. After you type a command, it is the responsibility of the shell to: (a) parse the command line; (b) handle wildcards, redirection, pipes, and job control; and (c) search for the command, and if found, execute that command. When you first learn Linux, you spend most of your time executing commands from the prompt. You will be using the shell interactively.
If you type the same set of commands on a regular basis, you may want to automate those tasks. To do so, you place each command in an executable file, called a shell script. A shell script is much like a batch file. More sophisticated scripts contain programming constructs for making decisions, looping, file testing, and so forth. Writing scripts not only requires learning programming constructs and techniques, but also assumes that you have a good understanding of Linux utilities and how they work. There are some utilities, such as grep, sed, and gawk, that are extremely powerful when used in scripts for the manipulation of command output and files. After you have become familiar with these tools and the programming constructs for your particular shell, you will be ready to start writing useful scripts. When executing commands from within a script, you will be using the shell as a programming language.
The Three Major UNIX Shells
The three prominent and supported shells on most UNIX systems are the Bourne shell (AT&T shell), the C shell (Berkeley shell), and the Korn shell (superset of the Bourne shell). All three of these shells behave pretty much the same way when running interactively, but have some differences in syntax and efficiency when used as scripting languages.
The Bourne shell is the standard UNIX shell, and the shell used to administer the system. Most of the system administration scripts, such as the rc start and stop scripts and shutdown, are Bourne shell scripts, and when in single-user mode, this is the shell commonly used by the administrator when running as root (superuser). This shell was written at AT&T and is known for being concise, compact, and fast. The default Bourne shell prompt is the dollar sign ($).
The C shell, developed at Berkeley, added a number of features, such as command line history, aliasing, built-in arithmetic, filename completion, and job control. The C shell has been favored over the Bourne shell by users running the shell interactively, but administrators prefer the Bourne shell for scripting, because Bourne shell scripts are simpler and faster than the same scripts written in C shell. The default C shell prompt is the percent sign (%).
The Korn shell is a superset of the Bourne shell written by David Korn at AT&T. A number of features were added to this shell above and beyond the enhancements of the C shell. Korn shell features include an editable history, aliases, functions, regular expression wildcards, built-in arithmetic, job control, coprocessing, and special debugging features. The Bourne shell is almost completely upward-compatible with the Korn shell, so older Bourne shell programs run fine in this shell. The default Korn shell prompt is the dollar sign ($).
The Major Linux Shells
The shells used by Linux do not exclusively belong to the Linux operating system. They are freely available and can be compiled on any UNIX system. But when you install Linux, you will have access to the Gnu shells and tools, not the standard UNIX shells and tools. Although Linux supports a number of shells, the Bourne Again shell (bash) and the TC shell (tcsh) are by far the most popular. The Z shell is another Linux shell that incorporates a number of features from the Bourne Again shell, the TC shell, and the Korn shell. The Public Domain Korn shell (pdksh) a Korn shell clone, is also available, and for a fee you can get AT&T's Korn shell, not to mention a host of other unknown smaller shells.
To see what shells are available under your version of Linux, look in the file, /etc/shell.
Uses of a Shell
One of the major uses of a shell is to interpret commands entered at the prompt. The shell parses the command line, breaking it into words (called tokens), separated by white space, i.e., tabs, spaces, or a newline. If the words contain special metacharacters, the shell evaluates them. The shell handles file I/O and background processing. After the command line has been processed, the shell searches for the command and starts its execution.
Another important function of the shell is to customize the user's environment, normally done in shell initialization files. These files contain definitions for setting terminal keys and window characteristics; setting variables that define the search path, permissions, prompts, and the terminal type; and setting variables that are required for specific applications such as windows, text-processing programs, and libraries for programming languages. The Bourne Again and the TC shells also provide further customization with the addition of history and aliases, filename and command completion, spell checking, help features, built-in variables set to protect the user from clobbering files, inadvertently logging out, and to notify the user when a job has completed, etc.
The shell can also be used as an interpreted programming language. Shell programs, also called scripts, consist of commands listed in a file. The programs are created in an editor (although online scripting is permitted). They consist of Linux commands interspersed with fundamental programming constructs, such as variable assignment, conditional tests, and loops. You do not have to compile shell scripts. The shell interprets each line of the script as if it had been entered from the keyboard. Because the shell is responsible for interpreting commands, it is necessary for the user to have an understanding of what those commands are.
Responsibilities of the Shell
The shell is ultimately responsible for making sure that any commands typed at the prompt get properly executed. Included in those responsibilities are:
-
Reading input and parsing the command line.
-
Evaluating special characters.
-
Setting up pipes, redirection, and background processing.
-
Handling signals.
-
Setting up programs for execution.