Setting Up History in Bash Shell

When you type a command on in a bash shell, it will be added to a history list. You can use the up and down cursor keys to see each command one at a time. There are also lots of other things you can setup and use to aid you when using the command line.

You can type ‘history’ and the list will be displayed. The numbers at the beginning of the line can be use to execute the command. Quick and simple ways to use the command line history.

  1. Use the up and down cursor keys.
  2. !ps to rerun the last ps command line you executed. If you remember the start of the command.
  3. !123 If you know the command number.
  439  ll /var/cache/apt-cacher/packages | wc -l
  440  more /etc/resolv.conf
  441  crisp  /home/richard/.ssh/known_hosts
  442  ssh linux60
  443  pdftotext ~/Desktop/lexyacc.pdf > ~/Desktop/Lexyacc.pdf
  445  top
  447  cd /usr/
  448  find ./ -name bcompare
  449  cd local/share
  455  cd lib/
  457  ll -d beyondcompare/
  458  cd
  459  h |p cpu

To run the command watch -n7 ntpq -cpe -cas from the above list use the following command.


Once you have used the command line for some time and built up a long history list it can be tedious to use the cursor keys or the history command to find the command you are after. The solution is the key combination CTRL+R. This allows you to search the history.

Using the history as an example, say we want to reuse the pdftotoext command again. But cannot remember the command but we do remember that the file was on the Desktop.

Pressing CTRL+R to start the search process off. As you type in the key word the history will be searched and the first command line that matches, working backwards up the list, will be display. So press
CTRL+R and type in Desk,

(reverse-i-search)`Des': pdftotext ~/Desktop/lexyacc.pdf > ~/Desktop/Lexyacc.pdf

There you go found it with ease. To use the command line press return. To quit press CTRL+C. You can even edit the command line, by pressing the left or right cursor keys.

HISTSIZE & HISTFILESIZE limit the size of the history list.

The two environment variables HISTSIZE and HISTFILESIZE limit the history size that you will have access to. HISTSIZE limits the entries in the history for any terminal or shell. Once this limit is reached older command lines will be lost until you type them again. The HISTFILESIZE limits the size of the history that
is saved between sessions. When you close shell window down the history is saved by default to your $HOME directory to a file called .bash_history. You can change the file name used with the HISTFILE variable.

These variables should go into your .bash_profile or .profile file. These commands are then executed once when you login. You can put them into your .bashrc but then they are unnecessarily executed every time
you start a shell. Even when the shell in not interactive, ie no command line.

Note: The defaults for these two are 500.


Using the HISTCONTROL variable you can stop some entries from being added such as duplicate entries.
I use the following value to stop any news lines matching from being added and also remove all
duplicated lines.

export HISTCONTROL=ignoreboth

It has the same effect as using both ignorespace and ignoredups. ignorespace will ignore lines which begin with a space so you can use this yourself to stop lines being added. ignoredups will stop duplicate being added if the same command
is used consecutively.

On later versions of bash there is a new option erasedups which causes all previous lines matching the current line to be removed from the history list before that line is saved.

HISTIGNORE variable.

One last variable HISTIGNORE allows you to set some coaamd patterns that will never
be added to the history. You might want to play around with this one but below is a starting example,


One thought on “Setting Up History in Bash Shell

Leave a Reply

Your email address will not be published. Required fields are marked *