78

If I perform a sequence of commands like:

$ ls
$ grep abc file.txt

and then use the up arrow key to get the previous one, the terminal will show the last cmd (which is the grep here)

But if I do something like this:

$ ls
$   grep abc file.txt

where grep is preceded by spaces, pressing up gives ls, not grep.

Why is this?

1

2 Answers 2

92
echo $HISTCONTROL
ignoreboth

If you want to change this behaviour add a new line to your ~/.bashrc file (which will affect every new shell you open):

HISTCONTROL=ignoredups

(assuming you still want to filter out duplicates)

man bash:

HISTCONTROL

A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.

6
  • 4
    and HISTCONTROL is set to ignoreboth in the default ~/.bashrc provided by debian at least. Feb 19, 2014 at 15:15
  • 2
    What is the use case of ignorespace? Sep 28, 2015 at 14:30
  • 2
    In Ubuntu 14.04 the default is ignoreboth which is kind-of annoying. May 9, 2016 at 8:01
  • 14
    Ciro, I assume it's so that you can enter command like echo "drop database mydb" | mysql -u root -pPassword and not have it saved in history.
    – Jeremy
    May 30, 2016 at 1:04
  • 2
    For mac users, HISTCONTROL is not set to anything, so you'll need to add it to your bash_profile or similar - unix.stackexchange.com/questions/115917/…
    – chrismarx
    Dec 26, 2018 at 19:00
-1

Add the line HISTCONTROL=ignoredups to ~/.bashrc (or /.bash_profile) then, from the command line, run source ~/.bashrc (or /.bash_profile) to implement the changes to the current shell session

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .