The Shell as a Command Line Interface


An operating system such as the Linux kernel consists of code written mostly in the low-level programming language C. The kernel is loaded into memory at the time the machine boots up, and sits there awaiting instructions.

You can pass instructions to the kernel in a number of ways. One of the most important is to write programs that interact with it ”once a program is written, you can compile and execute it to accomplish the task.

Because programs written to interact with the kernel often have several tasks in common, there is usually a library of these common tasks , which is a collection of C subroutines (or functions) organized into a library. This is simply known as the C library ( libc ). While programs that use the C library are powerful and flexible, it is often cumbersome, if not downright impractical , to write a program, compile it, and run it every time you need to do something as simple as copying a file.

What Is the Shell?

The shell is a declarative and parameterized mechanism for executing common tasks. What this means is that the shell allows you to execute programs that are generic enough to take parameters. For example, by using the file copy program cp and supplying a source file and a destination file as parameters (also known as arguments), it is possible to copy a file from one location to another in the filesystem. The power of the shell comes from the wide gamut of programs that can be executed from it. These programs are commonly known as UNIX commands . As you might have guessed by now, the shell itself is just another program.

In addition to allowing you to simply execute commands, the shell enables you to manage the execution of commands. It allows users to define shortcuts and abbreviations to refer to these commands and even to serve as a handy tool for knitting together commands to create conglomerates that function as new commands (they re called scripts).

In terms of flexibility, performance, task automation, and repetitive processing of tasks, the shell in the hands of a knowledgeable user still beats a GUI hands down.

Different Types of Shell

The earliest versions of UNIX came with what is today known as the Bourne shell ( sh ), named after its inventor , Steve Bourne. Since then, a variety of shell flavors have emerged. However, the core function- ality and basic syntax of all of these shells has changed very little.

The C-shell ( csh ), distributed as part of BSD UNIX, was the next popular version of the shell to arrive . The C-shell syntax resembles the C programming language (particularly when it comes to writing shell scripts), hence its name . This aspect of the C-shell was intended to popularize it among the UNIX users, who at that time were largely C programmers.

Other popular shells soon arrived, each with more features than the last, and all were backward compatible for the most part. Among them were the Korn shell (or ksh ), which was written by David Korn of AT&T Bell Laboratories, and tcsh or the C-shell with command-completion features ”that is, the shell intuitively completes the commands when you type them in.

The Born Again shell ( Bash ) is one of the newer versions of the shell, and is almost completely compatible with its ancestor , the Bourne shell.

Most UNIX systems have sh available apart from a preferred flavor of the shell for that UNIX system. In fact, some UNIX systems make several shells available, and the user can switch between them at will. As far as Linux (and therefore Fedora Core 2) is concerned , Bash is the preferred shell, although users may switch to other shell flavors (for example, you can switch to the C-shell by typing csh at the Bash prompt). The choice of a shell is often dictated by personal preference, sometimes by the fact that a user started off with a particular version, or sometimes, as in the case of the C-shell, the programming language the user is most comfortable with. This chapter uses the default shell, Bash, to illustrate various features of the shell.

You can figure out what your current shell is by typing the ps command in any terminal window at the command prompt. The ps command lists the processes ”that is, the currently executing programs on your system:

Note

The command prompt is depicted as $ for brevity; it is usually [user@localhost current directory] by default. You ll learn more about setting the prompt later in this chapter.

   $ ps   PID TTY          TIME CMD  5355 pts/0    00:00:00  bash  5381 pts/0    00:00:00 ps 

On executing the ps command, you should see an output similar to the one shown. On the last column of the second line of the output you see bash . If you were to switch to a different shell, such as csh , you would see csh in the output beside the Bash shell, which was used to launch it. Try it out by typing csh , followed by ps . (You look at the ps command in more detail in the next section.)

   $ csh     % ps   PID TTY          TIME CMD  5355 pts/0    00:00:00 bash  5389 pts/0    00:00:00 csh  5409 pts/0    00:00:00 ps 

Note that the prompt changed to a percentage sign ( % ) indicating that the current shell is C-shell. It should also be noted that switching to another shell doesn t mean you actually exit Bash and start csh; rather you use bash as the launch pad to start another shell, in this case csh .

You can, however, explicitly exit a shell. This can be achieved by using the exit command.

   $ csh     % ps   PID TTY          TIME CMD  5355 pts/0    00:00:00 bash  5389 pts/0    00:00:00 csh  5409 pts/0    00:00:00 ps % exit   $ ps   PID TTY          TIME CMD  5355 pts/0    00:00:00 bash  5409 pts/0    00:00:00 ps 

Note that after you execute the exit command, the command-prompt changes back to that of Bash. Also, the ps program does not list the C-shell any more because the C-shell terminated due to the exit command.




Beginning Fedora 2
Beginning Fedora 2
ISBN: 0764569961
EAN: 2147483647
Year: 2006
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net