Making use of command history on Linux

Command history on Linux allows you to review and repeat previous commands with little effort. It is built into the bash shell (included in the shell executable). It’s a helpful feature if you want to rerun commands without retyping them or examine recently run commands when you’re trying to determine when or how some recent change might have been made.

If you type “which history” on the command line, you can expect to see a response that indicates the shell can’t find it. Of course, that’s only because the which command is looking for an executable to run – not for a shell built-in.

This post takes a look at how the history command works and how to best use it. (See also: Bash command cheat sheet)

For starters, it’s helpful to understand that bash saves commands that you enter into a command history file called .bash_history. One of the important things to know about this file, however, is that you can type a command like the one below and then find that it’s not sitting at the bottom of your .bash_history file – at least, not yet:

$ echo Can you find me?
Can you find me?
$ tail -2 .bash_history
cd
cat myfile

At the same time, if you ask the history command to display recently entered commands, the command will be included in the output.

$ history | tail -3
1028 echo Can you find me?
1029 tail -2 .bash_history
1030 history | tail -3

This behavior is because bash stores commands in a buffer and only adds them to the history file when you log off or exit the shell. On your next login, the contents from your previous session will have been added.

How many commands are saved?

You can control the number of commands that will be saved in your history file. The default is usually 1000. To confirm the length of your command history, type the command shown below.

$ echo $HISTSIZE
1000

To change this to a different size, add a line to the bottom of your .bashrc with a command like those shown below that change the number of commands to be saved to 2,000. The 4th line sources the .bashrc file to adopt the change.

$ echo $HISTSIZE
1000
$ echo "HISTSIZE=2000" >> ~/.bashrc
$ . .bashrc
$ echo $HISTSIZE
2000

You can change the size if you find you’re saving too many or too few commands.

To change the default setting for $HISTSIZE, use root privilege and change the setting in /etc/profile. Note, however, that this will not change the setting for existing accounts.

Avoid saving duplicate commands

To avoid saving a command multiple times when entered some number of times in a row, use the HISTCONTROL option to ignore duplicates. To check your current settings, use these commands:

$ echo $HISTFILE
/home/justme/.bash_history
$ echo $HISTSIZE
1000
$ echo $HISTCONTROL
ignoredups

The settings shown display the name and location of the history file, the number of commands to be saved and that the ignoredups setting is already set up. If you type the whoami command three times in a row and then run the command shown below, you will only see the whoami command listed once.

$ history | tail -4
1049 echo $HISTFILE
1050 echo $HISTCONTROL
1051 whoami
1052 history | tail -4

Using your command history

To rerun a particular command that’s been saved in your history file, type an exclamation point followed by the line number from the file. Here’s an example:

$ !15
echo $HISTSIZE
1000

As shown above, the command will be displayed and then run.

You can also scroll through prior commands by pressing on the up arrow on your keyboard. This will move from the most recent command to the prior one, etc. Once you get to the one that you want to run, simply press the Enter key.  Note that you can make changes to the command before you press the enter key.

Another way to rerun a command is to type an exclamation point followed by the first letter(s) of the command that you want to rerun. This will choose the most recent command that matches the letters you type. Here’s a simple example:

$ echo 1
1
$ echo 2
2
$ echo 3
3
$ !e
echo 3
3

Ignoring commands not worthy of remembering

Command history can prove a very handy tool for rerunning commands – especially complex ones – without having to retype them. At the same time, many commands that you use often will prove not worthy of being remembered. The command below will ensure that the four commands included will *not* be remembered.

$ export HISTIGNORE='pwd:date:history:clear'

Once you use a command such as this, your command history will no longer include these commands. Here’s an example:

$ export HISTIGNORE='pwd:date:history:clear'
$ date
Thu Sep 26 03:57:08 PM EDT 2024
$ pwd
/home/newuser
$ history | tail -2
41 export HISTIGNORE='pwd:date:history:clear'
42 history | tail -3

Note that the commands included in the HISTIGNORE setting are no longer being saved.

$ history | tail -2
44 date
45 history | tail -2
$ export HISTIGNORE='pwd:date:history:clear'
$ date
Thu Sep 26 04:02:30 PM EDT 2024
$ pwd
/home/newuser
$ history | tail -2
46 export HISTIGNORE='pwd:date:history:clear'
47 history | tail -2

The date and pwd commands were run, but not added to the history command buffer once the HISTIGNORE setting was in place. Add the HISTIGNORE settings to your .bashrc file if you want it to remain active when you log in again.

Note that, given the settings shown above, use of the history command will not be added to your command history file. However, commands like “history | tail -10” will be saved. If you want to avoid this, add “history *” to your settings like this:

HISTIGNORE='pwd:date:history:history *:clear'

Wrap-up

The Linux history command can prove a very useful and time-saving tool. Controlling how many commands are saved and restricting which commands are saved may make reusing commands quite a bit more efficient.

Source:: Network World