Command History

Previously executed commands are stored in a history list. You can access this list to verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars and history also affect the history mechanism. There are a number of ways to use the history list:

  • Rerun a previous command

  • Edit a previous command

  • Make command substitutions

  • Make argument substitutions (replace specific words in a command)

  • Extract or replace parts of a command or word

The easiest way to take advantage of the command history is to use the arrow keys to move around in the history, select the command you want, and then rerun it or use the editing features described in Section 8.7.5, later in this chapter, to modify the command. The arrow keys are:

Key

Description

Up arrow

Previous command.

Down arrow

Next command.

Left arrow

Move left in command line.

Right arrow

Move right in command line.

The next sections describe some tools for editing and rerunning commands. With the C shell, which does not have the command-line editing features of tcsh, these tools are important for rerunning commands. With tcsh, they are less often used, but they still work.

Command Substitution

Command

Description

!

Begin a history substitution.

!!

Previous command.

! N

Command number N in history list.

!- N

Nth command back from current command.

! string

Most recent command that starts with string.

!?

Get Linux in a Nutshell, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.