84

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?

Anko
  • 4,526
sandyp
  • 1,177

2 Answers2

99
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.

redseven
  • 592
Hauke Laging
  • 90,279
-2

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