mv
, ls
, set
, and so on. However, some other commands are much longer, such as traceroute
or service – status-all
. Filenames can also be quite lengthy – up to 255 characters on many filesystems. Thus command completion can save a lot of time when you're typing. It can also help you avoid typos.
The most popular Linux shells, including bash
and tcsh
, support command and filename completion. Some older shells, though, don't support this helpful feature.
Another useful shell shortcut is history. The shell history keeps a record of every command you type. If you've typed a long command recently and want to use it again or use a minor variant of it, you can pull the command out of the history.
There are several rather easy methods to retrieve commands. It comes down to determining the method you like best:
Retrieve a Command
The simplest way to do this is to press the Up arrow key on your keyboard; this brings up the previous command. Pressing the Up arrow key repeatedly moves through multiple commands so you can find the one you want. If you overshoot, press the Down arrow key to move down the history. The Ctrl+P and Ctrl+N keystrokes double for the Up and Down arrow keys, respectively.
Search for a Command
Press Ctrl+R to begin a backward (reverse) search, and begin typing characters that should be unique to the command you want to find. The characters you type need not be the ones that begin the command; they can exist anywhere in the command. You can either keep typing until you find the correct command or, after you've typed a few characters, press Ctrl+R repeatedly until you find the one you want.
The Ctrl+S keystroke is used to search forward in the command history. You can press the Ctrl+S keystroke while using the backward search. This reverses the history search from backward to forward. If you used a backward search and have passed by what you need, then this keystroke is useful.
If the Ctrl+S keystroke causes your terminal to hang, press Ctrl+Q to resume terminal operations. To keep your terminal from hanging when Ctrl+S is used, type stty – ixon at the command line.
In either event, if you can't find the command you want or if you change your mind and want to terminate the search, press Ctrl+G to do so.
Frequently, after finding a command in the history, you want to edit it. The bash
shell, like many shells, provides editing features modeled after those of the Emacs editor:
Move within the Line
Press Ctrl+A or Ctrl+E to move the cursor to the start or end of the line, respectively. The Left and Right arrow keys move within the line a character at a time. Ctrl+B and Ctrl+F do the same, moving backward and forward within a line. Pressing Ctrl plus the Left or Right arrow key moves backward or forward a word at a time, as does pressing Esc and then B or F.
Delete Text
Pressing Ctrl+D or the Delete key deletes the character under the cursor. Pressing the Backspace key deletes the character to the left of the cursor. Pressing Ctrl+K deletes all text from the cursor to the end of the line. Pressing Ctrl+X and then Backspace deletes all of the text from the cursor to the beginning of the line.
Transpose Text
Pressing Ctrl+T transposes the character before the cursor with the character under the cursor. Pressing Esc and then T transposes the two words immediately before (or under) the cursor.
Change Case
Pressing Esc and then U converts text from the cursor to the end of the word to uppercase. Pressing Esc and then L converts text from the cursor to the end of the word to lowercase. Pressing Esc and then C converts the letter under the cursor (or the first letter of the next word) to uppercase, leaving the rest of the word unaffected.
Invoke an Editor
You can launch a full-fledged editor to edit a command by pressing Ctrl+X followed by Ctrl+E. The bash
shell attempts to launch the editor defined by the $FCEDIT
or $EDITOR
environment variable, or it launches Emacs as a last resort.
These editing commands are just the most useful ones supported by bash
. In practice, you're likely to make heavy use of command and filename completion, the command history, and perhaps a few editing features.
If you prefer the vi editor to Emacs, you can use a vi-like mode
in bash by typing set – o vi. (vi is described in Chapter 5, “Booting Linux and Editing Files.”)
The history
command provides an interface to view and manage the history. Typing history alone displays all of the commands in the history (typically the latest 500 commands).
To retrieve the last command in your shell history, type !! and press Enter. This will not only show you the command you recalled but execute it as well:
You can execute a command by number via typing an exclamation mark followed by its number, as in !210 to execute command 210. Typing history – c clears the history, which can be handy if you've recently typed commands you'd rather not have discovered by others, such as commands that include passwords.
The bash
history is stored in the .bash_history
file in your home directory. This is an ordinary plain-text file, so you can view it with a text editor or a command such as less
(described later, in “Paging through Files with less
”).
Because your bash
history is stored in a file, it can be examined by anybody who can read that file. Some commands enable you to type passwords or other sensitive data on the same line as the commands themselves, which can therefore be risky. The ∼/.bash_history file does not record what you type in response to other programs' prompts, just what you type at the bash prompt itself. Thus, if you have a choice, you should let commands that require passwords (or other sensitive data) prompt you to enter this data rather than enter such information as options to the command at the bash
prompt.
In Exercise 1.1, you'll experiment with your shell's completion and command-editing tools.
Exercise 1.1
Editing Commands
To experiment with your shell's completion and command-editing tools, follow these steps:
1. Log in as an ordinary user.
2. Create a temporary directory by typing mkdir test. (Directory and file manipulation commands are described in more detail in Chapter 4.)
3. Change into the test directory by typing cd test.
4. Create a few temporary files by typing touch one two three. This command creates three empty files named one
, two
, and three
.
5. Type ls – l t and, without pressing the Enter key, press the Tab key. The system may beep at you or display two three
. If it doesn't display two three
, press the Tab key again and it should do so. This reveals that either two
or three
is a valid completion to your command, because these are the two files in the test directory whose filenames begin with the letter t
.
6. Type h, and again without pressing the Enter key, press the Tab key. The system should complete the command (ls – l three
), at which point you can press the Enter