-->
Previous Table of Contents Next


Chapter 11
bash

by Rick McMullin and Tim Parker

In This Chapter
•   Shells in a nutshell
•   The Bourne again shell
•   Customizing bash
•   bash command summary

This chapter and the two that follow look at the shells available to you under Linux in a little more detail. Most Linux systems come with several shells, including bash, pdksh, and tcsh. We’ll start with bash (Bourne Again Shell) because it’s the default shell used by Linux and the most popular shell for new users. We’ll also look at the most commonly used bash commands and the environment variables bash uses. By the end of this chapter, you should be able to work faster and more efficiently with bash.

Shells in a Nutshell

What is a shell, anyway? It seems to be a word used all the time in Linux, but the exact meaning is vague for many new users (and some veterans). This section explains what a shell program is and why it is so important when using Linux.

What Is a Shell?

The shell is a program used to interface between you (the user) and Linux (or, more accurately, between you and the Linux kernel). Figure 11.1 illustrates the relationship between the user, the shell, and the Linux kernel. Every command you type at a prompt on your screen is interpreted by the shell, then passed to the Linux kernel.


Figure 11.1.  The relationship between the user and the shell.


Note:  
If you are familiar with MS-DOS, you will recognize this relationship as almost identical to the relationship between a DOS user and the COMMAND.COM program. The only real difference is that in the DOS world, no distinction is made between the COMMAND.COM program and DOS (or to be more accurate, the DOS kernel).

The shell is a command-language interpreter. It has its own set of built-in shell commands. The shell can also make use of all of the Linux utilities and application programs that are available on the system.

Whenever you enter a command it is interpreted by the Linux shell. For example, in earlier chapters when you were introduced to the Linux file- and directory-manipulation commands, all of the sample commands entered at the command prompt were interpreted by whichever Linux shell you were using.

Some of the commands, such as the print working directory (pwd) command, are built into the Linux bash shell. Other commands, such as the copy command (cp) and the remove command (rm), are separate executable programs that exist in one of the directories in the file system. As the user, you don’t know (or probably care) if the command is built in to the shell or is a separate program. Figure 11.2 shows how the shell performs this command interpretation.


Figure 11.2.  Command interpretation by the shell.

Figure 11.2 illustrates the steps that the shell takes to figure out what to do with user commands. It first checks to see whether the command is one of its own built-in commands (such as cd or pwd). If the command is not one of these built-in items, the shell checks to see if it is an application program. Application programs can be utility programs that are part of Linux, such as ls and rm, or they can be application programs that are either purchased commercially, such as xv, or are available as public domain software, such as ghostview.

The shell tries to find these application programs by looking in all of the directories that are in your search path. As we mentioned in an earlier chapter, the path is a list of directories where executable programs can be found. If the command that is entered is not an internal shell command and is not an executable file in your path, an error message is displayed similar to this one:


$ doit

doit: not found

As the last step in running a successful command, the shell’s internal commands and all of the application programs are eventually broken down into system calls and passed to the Linux kernel.

Another important aspect of the shell is that it contains a very powerful interpretive programming language. This language is similar in function to the MS-DOS interpreted language, but is much more flexible. The shell programming language supports most of the programming constructs found in high-level languages, such as looping, functions, variables, and arrays.

How the Shell Gets Started

Earlier in this chapter you learned that the shell is the main method through which a user interacts with the Linux kernel. But how does this program get initialized to do so? The shell is started after you successfully log in to the system, and it continues to be the main method of interaction between the user and the kernel until you log out.

Each user on your system has a default shell. The default shell for each user is specified in the system password file, called /etc/passwd. The system password file contains, among other things, each person’s user ID, an encrypted copy of each user’s password, and the name of the program to run immediately after a user logs in to the system. The program specified in the password file does not have to be one of the Linux shells, but it almost always is.


Previous Table of Contents Next