History Command in Linux (Bash History)

Posted 

5 min read

If you spend a lot of time on the command line, viewing the history of the commands you have previously run could be a useful feature that can make your day to day work more easier and improve your productivity.

In this article, we will talk about the history command, which allows you to view a list of previously executed commands, search through the list, and manipulate the history file.

Using the history Command and History Expansions

history is a shell builtin, and its behavior may slightly differ from shell to shell. We will cover the Bash builtin version of history.

In it’s simplest form, when invoked without any option or argument, the history command displays the whole history list with line numbers.

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

Typing !n executes the n-th command from the history list, and !-n the command n lines back. In the following example we’re executing the command on line 467:

!467

Another way to execute a command is to use !word expansion. word refers to the most recent command starting with ‘word’.

Typically, history displays many lines of output that doesn’t fit on the screen. To view the output one page at a time, pipe it to a pager program like more or less command:

history | less

To display the last n lines, pass the number as an argument to the command. For example, to view only the last five lines from the history list you would type:

history -5

Use the up and down arrow keys to navigate the entries in the list. When the command you’ve searched for is shown press Enter to execute it.

Type !! to execute the previous command:

!!

This is especially usefully when you forget to perpend a command with sudo, and instead of re-typing the command you can type:

sudo !!

!-1 is the same as !! and executes the last command from the history list, !-2 second to last, and so on.

^word1^word2^ expansion allows you re-run the last command replacing “word1” with “word2”. If you accidentally typed sduo command instead of sudo command you can repeat the command using the correct word with:

^sduo^sudo^

Use the grep command to filter the output. For example, to view all commands including “nano” you would run:

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

Now, if you want to re-run the nano setup.py command simply type:

!356

Another way to search through the command history is by pressing Ctrl-R. The prompt will change to the following, and you can start searching for a previously executed command.

(reverse-i-search)`':

The shell will display a matching line. To move to the next matched suggestion, press Ctrl-R again.

Check the Bash manual for more information about History Expansion, modifiers, and designators.

Saving the History List

By default, when starting a new session, Bash reads the history list from the .bash_history file . The list of commands that are executed in the current session are kept in the memory and saved to the file when the session is closed.

If you opened several shell sessions, only the history of the session that is closed last is saved.

The -a option allows you to save the current session history list to the .bash_history file:

history -a

The -w option writes the complete history list to the history file.

history -w

Clearing History

The history command allows you to clear the complete history list or remove certain parts.

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

history -c

To delete a specific line or lines between a start and end positions from the history list, use the -d option.

For example, to remove the lines between 365 and 375 (including those lines), you would type:

history -d 365 375

If you provide only one number to the -d option, the command removes the given line.

When a negative integer is used, the lines are count back from the end of the history list.

The commands above clear the history list, which is kept in the memory, but does not remove entries from the .bash_history file on the disk. To clear the file, you need to write the history list to the file:

history -c
history -w

Modifying History Behavior

The behavior of the Bash history can be defined using several different environment variables. When modifying the history behavior, set the variables in ~/.bashrc or any other configuration file which is loaded when the user logs in.

By default Bash keeps 500 lines in the command history list. The HISTSIZE variable allows you to change this value. To set it to 10000 add the following line to your .bashrc file:

HISTSIZE=10000

The HISTCONTROL variable accepts a colon-separated list of values that define how the commands are saved in the history list: - ignorespace - commands that start with space are not saved in the history list. - ignoredups - duplicate commands are not saved. - ignoreboth - is a shorthand, including both ignorespace and ignoredups.

HISTCONTROL=ignoreboth

When the HISTTIMEFORMAT variable is set, Bash prepends a timestamp of execution for the command on each line.

For example, if you set:

HISTTIMEFORMAT="%F %T: "

The history will be displayed in the following format:

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

Conclusion

The history command displays a list of previously executed commands. For example, you can use the history to view a long command you’ve used before that you can’t remember.

If you have any questions or feedback, feel free to leave a comment.