History Command in Linux (Bash History)

Introduction

If you spend a lot of time at the command prompt, checking the history of commands you've executed before could be a handy tool that makes your day-to-day job easier and more productive.

In this tutorial, we will discuss the history command, which allows you to display a list of previously run commands, search through the list, and alter the history file. We will also address a few FAQs on history command in Linux (Bash History).

Using the history Command and History Expansions

history is a built-in feature of the shell, and its behavior may vary significantly from one shell to another. We'll go over Bash's built-in history.

When run without any options or arguments, the history command displays the whole history list with line numbers in its most basic form.

history
Output

...
467  git push
468  tail -f var/logs/error
469  nano +22,5 functions.sh
470  source project-env/bin/activate
471  history

!n runs the nth command from the history list, while  !-n runs the command n lines back. In the example below, we'll run the command on line 467:

!467

The !word expansion command is another way to run a command. The most recent command that begins with 'word' is referred to as word.

The output of history generally contains a significant number of lines that do not fit on the screen. To view it one page at a time, pipe the output to a pager application like more or less command.

history | less

Pass the number as an input to the command to display the latest n lines. For example, if you simply want to see the last five lines from the history list, type:

history 5

To navigate through the list, use the up and down arrow keys. When the command you're looking for appears, hit Enter to run it.

To run the preceding command, type !!:

!!

This is extremely useful if you forget to prefix a command with sudo and instead of retyping it, you can type:

sudo !!

!! and !-1 are the same thing and runs the most recent command in the history list, !-2 second to last, and so on.

You can re-run the previous command by changing "word1" with "word2" using the ^word1^word2^ expansion. If you wrote sduo command instead of sudo command by accident, you can rerun the command with the right word:

^sduo^sudo^

To filter the output, use the grep command. To see all instructions that include "nano" for example, type:

history | grep nano
302  sudo nano /etc/resolv.conf
356  nano setup.py
413  sudo nano /etc/hosts
469  nano +22,5 functions.sh

Simply type the following command to re-run the nano setup.py program:

!356

Ctrl-R can also be used to search through the command history. The prompt will change to the following, and you can begin looking for a command that was previously run.

(reverse-i-search)`':

A corresponding line will be displayed by the shell. Press Ctrl-R again to get to the next matched proposal.

For additional information on History Expansion, modifiers, and designators, consult the Bash manual.

Saving the History List

Bash reads the history list from the .bash_history file by default when beginning a new session. The list of commands that were performed in the current session is retained in memory and written to a file when the session is closed.

If you open several shell sessions, just the history of the most recent shell session is saved.

You can store the current session history list to the .bash_history file with the -a option:

history -a

The -w option saves the entire history list to the file history.

history -w

Clearing History

With the history command, you can clear the full history list or only parts of it.

To clear the history list, use the -c option:

history -c

Between a start and completion point, use the -d option to remove a specific line or lines from the history list.

To delete the lines between 365 and 375 (including those lines), for example, type:

history -d 365 375

When you use the -d option with only one number, the program eliminates the specified line.

When a negative number is specified, the lines are tallied backwards from the end of the history list.

The commands above clear the history list in memory, but they do not erase items from the .bash_history file on the hard drive. To clear the file, write the following history list to it:

history -c
history -w

Modifying History Behavior

Several distinct environment variables can be used to define the behavior of the Bash history. Set the variables in ~/.bashrc or any other configuration file that is loaded when the user comes in to change the history behavior.

The command history list in Bash is limited to 500 lines by default. This value can be changed using the HISTSIZE variable. Add the following line to your .bashrc file to set it to 10000:

HISTSIZE=10000

The HISTCONTROL variable accepts a list of values separated by colons that define how instructions are saved in the history list:

  • ignorespace - The history list does not save commands that begin with a space.
  • ignoredups - Commands that are duplicated are not preserved.
  • ignoreboth - ignorespace and ignoredups are both ignored in this shorthand.
HISTCONTROL=ignoreboth

Bash prepends a timestamp of execution for the command on each line when the HISTTIMEFORMAT variable is specified.

For instance, if you specify:

HISTTIMEFORMAT="%F %T: "

The following format will be used to display the history:

413  2019-10-27 21:13:07: sudo nano /etc/hosts

FAQs on history command in Linux (Bash History)

How do I use the history command to view my command history? 

Simply type history on your command line and press Enter. This will display a numbered list of your previously executed commands, with the most recent ones at the bottom.

Can I limit the number of commands displayed by the history command? 

Yes, you can specify the maximum number of commands to display by using the -n option followed by a number. For example, history -n 20 will display the last 20 commands.

How can I search for a specific command in my command history using the history command? 

To search for a specific command, use the -grep option followed by the keyword you are looking for. For example, history | grep "keyword" will display a list of commands containing the specified keyword.

Can I execute a previously executed command from my command history using the history command? 

Yes, you can execute a previously executed command by using the ! notation followed by the command number. For example, !77 will execute command number 77 from your command history.

Is it possible to clear the command history using the history command? 

Yes, you can clear your entire command history by using the -c option with the history command. For example, history -c will clear all previously executed commands from your history.

How can I save my command history to a file using the history command? 

To save your command history to a file, use the -w option followed by the filename. For example, history -w history.txt will save your command history to a file named "history.txt".

Conclusion

The history command provides a list of commands that have been run earlier. You can utilize the history, for example, to look up a long command you've used before but can't recall.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.