Bash in Fedora 19 is really annoying me. Say I run a command:

echo "Hello"

Then I run another command:

echo "World"

Now I run the last command (echo "World") again N times by pressing up on my keyboard, followed by enter. (Typical usage.)

The problem is that every one of those echo "World" commands is inserted into the history, even though they are identical. I want to simply press up twice and get to echo "Hello". Instead, I must press up N+1 times.

Why is this broken? How do I fix it?

| |
  • It's not broken - each command is entered into history. – suspectus Apr 8 '14 at 13:16

To uniqely record every new command is tricky. First you need to add to ~/.profile or similar:

PROMPT_COMMAND='history -w'

Then you need to add to ~/.bash_logout:

history -a
history -w
| |
  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Feb 4 '18 at 20:26

I don't know how to fix this issue but I'm gonna give you an alternative by far better. Do it with "Ctrl+r echo" and iterate through your typed versions with "Ctrl+r" It's faster and when you get the trick, you'll love it.


Ctrl+r echo

the promt show: "echo World", if this is not what you want, push one more time Ctrl+r

the promt show: "echo hello", if this is not what you want, push Ctrl+r. If this is what you want, simply push Enter.

And you don't need to start your word in order, you can do:

Ctrl+r World

and the prompt will show "echo World" and you can push Enter to execute it.

So easy.

| |

This is 'broken' because every command is entered into the history by default, as @suspectus has pointed out.

You can, however, instruct Bash to erase duplicates by setting the Bash variable HISTCONTROL. (Documentation: http://www.gnu.org/software/bash/manual/bashref.html#Bash-Variables). It's a colon-separated list of zero or more keywords, the one you are most likely interested in being erasedups. Note that only history entered in the current sessions is affected. Setting HISTCONTROL=erasedups won't go back and erase duplicates from your former sessions (but do look below for another solution). Also note that HISTCONTROL is a Bash variable and doesn't need to be exported.

For what it's worth, my settings are:


I started using these settings because I was annoyed by many trivial and duplicate commands pushing carefully crafted commands 'off the list'. The HISTIGNORE setting ignores short commands. I find it's not much shorter to type 'Up Arrow' to retrieve a command like ls, so I prefer not to store one- to three-letter commands at all. Also, anything starting with a space will not be stored in the history. I use this for throw-away commands.

Note that duplicates may still accumulate if you have multiple sessions open at the same time. From time to time, I remove duplicates from the history file with the following one-liner:

tac ~/.bash_history | awk '!seen[$0]++' | tac > .tmp.newhist && mv .tmp.newhist ~/.bash_history
| |

Bash history cannot distinguish duplicated commands. All commands are recorded in history. One way to quickly get to the command required is to:

set -o vi

type ESC, then /hello will give you the most recent command with the string "hello". Use the up arrow to list previous commands matching the string "hello". Other vi commands can be used to retrieve / navigate commands.

If useful set -o vi can be added to .bashrc in your HOME directory.

| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.