X

Tip: set the Terminal to store only unique commands in the history

When you enter commands in the Terminal, they are saved in a history that you can scroll through by pressing the up and down arrows. This tip prevents the same item from being saved to the history over and over again.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
2 min read

When you enter commands in the Terminal, they are saved in a history that you can scroll through by pressing the up and down arrows. This is convenient because you can quickly access previously entered commands for repetitive tasks, or to access previously used syntax for a command. While this is useful, if you have entered the same command several times you will have to scroll through the same command in the terminal in order to get to a unique command you entered. This default behavior can be changed, which may be convenient for people who access the terminal frequently.

To alter the behavior of the terminal so it will only store unique commands and not keep a series of the same command in its history, you will need to add an argument to the shell's profile. A Unix shell is an environment in which terminal applications and commands are run. There are a variety of them, with Bash or "Bourne Again Shell" being the one that is used by default for users in OS X. In order to customize the Bash history to not record duplicate commands, follow this procedure:

  1. Launch the Terminal

  2. Enter the following command to edit the bash history (I use "pico" by habit, but it's really "nano"):

    pico ~/.bash_profile

  3. When the editor opens, press the down arrow to get to the last line, and add the following to the file:

    export HISTCONTROL=ignoredups

  4. When finished, press control-O to save the document (confirm overwriting), then press control-X to close it.

  5. Quit and relaunch the Terminal.

The Bash profile opened in the pico/nano text editor Screenshot by Topher Kessler

When this is done, you should now be able to enter the same command multiple times without all the same entries being saved to the command history, making command navigation much easier in some cases.

For more information about how to customize and use Bash (or most other Unix commands), open the terminal and enter "man bash," or see the online manual page.