home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Previous SectionNext Section

7.1 Issuing Shell Commands

The most common way to access the shell is via a terminal window, as explained in Chapter 5 and Chapter 6. However, a terminal window isn't the only way to access the shell. Section 4.3.3.2 explains how to access the shell by using a virtual console.

As your first Linux command, launch a terminal window, type w, and press Enter. Your contents of the terminal window should look something like this:

[bill@home bill]$ w
11:12am  up 6 min,  1 user,  load average: 0.00, 0.08, 0.05
USER     TTY      FROM      LOGIN@   IDLE   JCPU   PCPU  WHAT
bill     tty1              11:11am  0.00s  0.20s  0.11s  -bash

The w command tells Linux to display the system status and a list of all system users. In the example, the output of the command tells you that it's now 11:12 a.m., that the system has been up for six minutes, and that only one user—bill—is currently logged in. Notice that the command output is very terse, packing much information into a few lines. Such output is typical of Linux commands. At first, you may find Linux output cryptic and difficult to read, but over time you'll grow to appreciate the efficiency with which Linux communicates information.

Linux command output is not terse owing to an oversight or laziness on the part of the creators of Linux. Instead, Linux command output is designed so that it can be processed by programs as well as by humans. The structure of the output simplifies the task of programmers who write programs to process command output.

Linux provides many commands besides the w command—so many that you may despair of learning and recalling them. Actually, the number of commands you'll use regularly is fairly small. Soon, they will become second nature to you.

Try a second command, the date command:

[bill@home bill]$ date
Fri Oct 5 11:15:20 PST 2002

The date command displays the current date and time.

If you find working with MS-DOS distasteful or intimidating, you may not immediately enjoy working with the Linux command line. However, give yourself some time to adjust. The Linux command line has several features that make it easier to use, and more powerful, than MS-DOS.

7.1.1 Correcting Commands

Sometimes you may type a command incorrectly, causing Linux to display an error message. For example, suppose you typed dat instead of date:

[bill@home bill]$ dat
bash: dat: command not found

In such a case, carefully check the spelling of the command and try again. If you notice an error before pressing Enter, you can use the Backspace or Left arrow key to return to the point of the error and then type the correct characters. The Backspace key erases characters whereas the Left arrow key does not. You can also use the Del key to delete unwanted characters.

Just as a web browser keeps track of recently visited sites, the bash shell keeps track of recently issued commands in what's known as the history list. You can scroll back through bash's history by using the Up arrow key, or back down using the Down arrow key, just as you would with the Back and Forward buttons on a web browser. To reissue a command, scroll to it and press Enter. If you like, you can modify the command before reissuing it. When typing shell commands, you have access to a minieditor that resembles the DOSKEY editor of MS-DOS. This minieditor lets you revise command lines by typing key commands. Table 7-1 summarizes some useful key commands interpreted by the shell. The key commands let you access a list of the 500 most recently executed commands. bash's history is saved in the ~/.bash_history file.

Table 7-1. Useful editing keystrokes

Keystroke(s)

Function

Up arrow

Move back one command in the history list.

Down arrow

Move forward one command in the history list.

Left arrow

Move back one character.

Right arrow

Move forward one character.

Backspace

Delete previous character.

Tab

Attempt to complete the current word, interpreting it as a filename or command, as determined by thecontext.

Alt-B

Move back one word.

Alt-D

Delete current word.

Alt-F

Move forward one word.

Ctrl-A

Move to beginning of line.

Ctrl-D

Delete current character.

Ctrl-E

Move to end of line.

Ctrl-K

Delete to end of line.

Ctrl-L

Clear the screen, placing the current line at the top of the screen.

Ctrl-U

Delete from beginning of line.

Ctrl-Y

Retrieve last item deleted.

Esc .

Insert last word of previous command (note that Esc is pressed before the dot, rather than at the same time).

Esc ? or Tab

List the possible completions (note that Esc is pressed before the question mark, not at the same time).

One of the most useful editing keystrokes, Tab, can also be used when typing a command. If you type the first part of a filename and press Tab, the shell will attempt to locate files with names matching the characters you've typed. If something exists, the shell fills out the partially typed name with the proper characters. You can then press Enter to execute the command or continue typing other options and arguments. This feature, called either filename or command completion, makes the shell much easier to use.

In addition to keystrokes for editing the command line, the shell interprets several keystrokes that control the operation of the currently executing program. Table 7-2 summarizes these keystrokes. For example, typing Ctrl-C generally cancels execution of a program. This keystroke command is handy, for example, when a program is taking too long to execute and you'd prefer to try something else.

Table 7-2. Useful Control keystrokes

Keystroke

Function

Ctrl-C

Sends an interrupt signal to the currently executing command, which generally responds by terminating itself.

Ctrl-D

Sends an end-of-file to the currently executing command; use this keystroke to terminate console input.

Ctrl-Z

Suspends the currently executing program.

Several other special characters control the operation of the shell, as shown in Table 7-3. The # and ; characters are most often used in shell scripts, which you'll learn about in more detail later in this chapter. The & character causes the shell prompt to return immediately instead of waiting for a command to finish; the command runs in the background and you can continue to enter more commands.

Table 7-3. Other special shell characters

Character

Function

#

Marks the line as a comment, which the shell ignores.

;

Separates commands, letting you enter several commands on a single line.

&

Placed at the end of a command, causes the command to execute as a background process, so that a new shell prompt appears immediately after the command is entered.

>

Stores output from the command in the file whose name follows.

<

Takes input from the file whose name follows.

\

At end of line, continues command on the following line.

    Previous SectionNext Section