Linux/Unix – Command History

 

Your command history is the sequential list of commands you have typed, in the current or previous shell sessions. The commands in this history list are called events.

By default, bash remembers the last 500 events, but this number is configurable. Your command history is stored in a text file in your home directory called `.bash_history’; you can view this file or edit it like you would any other text file.

Two very useful things that having a command history lets you do is to repeat the last command you typed, and (as explained earlier in this chapter) to do an incremental backwards search through your history. 

The following sections explain how to view your history and specify events from it on the command line. See Info file `bashref.info’, node `Bash History Facilities’, for more information on command history.

 

Viewing Your Command History

Use history to view your command history. To view your command history, type:

$ history

1 who

2 apropos shell 62;shell−commands

3 apropos bash 62;bash−commands

4 history

$

 

This command shows the contents of your command history file, listing one command per line prefaced by its event number. Use an event number to specify that event in your history. If your history is a long one, this list will scroll off the screen, in which case you may want to pipe the output to less in order to peruse it. It’s also common to search for a past command by piping the output to grep. To search your history for the text `apropos’, type:

 

$ history | grep apropos

2 apropos shell 62;shell−commands

3 apropos bash 62;bash−commands

5 history | grep apropos

$

This command will show the events from your history containing the text `apropos’. (The last line of output is the command you just typed.)

 

Specifying a Command from Your History

You can specify a past event from your history on the input line, in order to run it again. The simplest way to specify a history event is to use the up and down arrow keys at the shell prompt to browse your history. The up arrow key (@uparrow) takes you back through past events, and the down arrow key (@downarrow) moves you forward into recent history. When a history event is on the input line, you can edit it as normal, and type RETURN to run it as a command; it will then become the newest event in your history.

[GNU INFO BUG: any <> in the preceding line should be the one of the cursor arrow keys.] To specify the second−to−the−last command in your history, type:

 

$ @uparrow @uparrow

[GNU INFO BUG: any 60;62; in the preceding line should be the one of the cursor arrow keys.]

To run a history event by its event number, enter an exclamation point (`!’, sometimes called “bang”) followed by the event number. To run history event number 1, type: 

$ !1

 

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 15, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: