Bash Command History

Bash maintains a list of the commands you have entered at the shell prompt. You can view the contents of this list by issuing the history command. Pass a number as an argument to the command to specify how many history entries you would like to view. The following will display the last 25 commands you have entered at the shell prompt:

history 25

Navigating and Using History Entries

When you are at the command prompt you can press the up arrow to see the most recent command entered. You can continue to press the up arrow key to see earlier commands, and press the down arrow key to move through the list back toward the most recent command and back to the prompt.

While navigating through the history list you can edit a command, and you can press enter to execute the command.

Entries from the history list are displayed with line numbers which you can use to reissue the command by proceeding it with an exclamation point. The following will execute command number 12 in the history list:

!12

Searching History

You can search for commands in your history list by pressing Ctrl-r and beginning to type the text to search for. The search is refined as you continue to enter characters. You can press Ctrl-r repeatedly to search backwards in history. When you press tab or arrow keys or otherwise navigate within the command line, that command is then placed at the prompt where you can edit it and/or press enter to execute it. Ctrl-j also places the command at the prompt to edit or execute. To end the search and return to the shell prompt, press Ctrl-c or Ctrl-g.

You can also use the grep command to search history entries. The following example uses grep to search the history list for commands that include the pattern ssh:

history | grep ssh

History File, Variables, and Options

By default, each user's command history is contained in the .bash_history file in their home directory. You can verify this by inspecting the value of the HISTFILE variable as follows:

echo $HISTFILE

The HISTSIZE and HISTFILESIZE variables control the number of entries in the history file and its file size.

Commands that you enter at the shell prompt are saved into the history file when you end the session. Options to the history command allow you to clear all the entries (-c), delete particular entries (-d n), write the current history list to the history file (-w) and more. The following command will empty your history file of previous commands:

history -cw

Back to top