Bash Command History – Why Bash Does Not Store Commands Starting with Spaces

bashcommand historywhitespace

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?

Best Answer

echo $HISTCONTROL
ignoreboth

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.