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 key to return to the point of the error and then type the correct 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, 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, username, variable name, or command as determined by the context.

Ctrl-A

Move to beginning of line.

Ctrl-D

Delete current character.

Ctrl-E

Move 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.

Esc ?

List the possible completions.

Esc b

Move back one word.

Esc d

Delete current word.

Esc f

Move forward one word.

Esc k

Delete to end of line.

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 completion 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 is useful for running a command as a background process.

Table 7-3. Other Special Shell Characters

Character

Function

#

Marks the command 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.60.13