Site Navigation

Nov 1, 2011

Bash Command Line History Tricks

Linux LogoThis week in a training course I was using a Red Hat based Linux that had the up arrow assigned to taking a snapshot of the screen. Since I like to use the up and down arrows to navigate command lines in the Bash shell this was a bit of a bummer. However, I vaguely remembered some alternatives. Here is what I find.

EMACS Commands
By default Bash supports the use of EMACs commands to navigate through your command line history. You can move backward through the command history using Ctl-p and move forward through it with Ctl-n. Pretty much replaces the arrows.

In addition, I found out you can use Ctl-r to search through the history. After typing Ctl-r, enter the letters you are searching for and the command that best matches, is displayed. You can also modify the matched command line.

History Commands
In addition to EMACs, there are a number of commands that work with the command line history. You can use !! to run the previous command and !3 to run the command on line 3 of the history list.

I also figured out if you echo the command, (e.g., echo !5) you can use Ctl-p to go back and edit the line.

Movement Commands

Ctl-p - Display previous command line
Ctl-n - Display next command line
Ctl-a - Move to the start of the line
Ctl-e - Move to the end of the line


Search Command Lines

Ctl-r - Searches backward through the history for the letters you type
Just start typing the text you are searching for. The closest match will be displayed. The matched command line can be edited and modified.

History Command


history - Display the command history
!! - Repeat the last command
!# - Repeat the line number in the history
echo !! - Echo the previous command to standard out

Post a Comment

Favorite Links Feed