Bash History broken, up arrow cycles to duplicates from bashrc

EDIT: Partial solution for now is to call exec zsh in my bashrc and call it a day. I don’t like it but history behaves normally at least.
I have been trying to fix this for ages.

Adding HISTCONTROL=ignoredups or HISTCONTROL=erasedups does not work as pressing the Up arrow key goes to last environment variable in my bashrc. My bash_history duplicates bashrc for every shell I open. It has been a few months since I actually used my history.

The first few lines of my bashrc are as follows:

set -o history
export HISTFILE=~/.bash_history
export HISTCONTROL=ignoredups
export HISTSIZE=100000
export SAVEHIST=100000

I had copied over the .histfile when I was using zsh. Zsh broke for me a while back, wouldn’t let me log in or use anythin in /bin so I reverted to Bash but the history is still messed up. I can still ctrl-R and find commands, but it’s suboptimal, I heavily rely on History to get any work done. Any help would be awessome! Thanks!

Try adding following to you ~/.bashrc

export HISTCONTROL=ignoreboth:erasedups

Another solution is to sort existing ~/.bash_history using sort and uniq commands and overwrite it.

1 Like

I tried both suggestions. But with every second new terminal I open, the bashrc duplicates again into bash_history.

Before your sort suggestions, I manually removed duplicates (luckily I didn’t have much history to go through). But this too did not work. Overwriting is fine but with a new terminal bashrc duplicates take over the latest commands :frowning:

The whole problem began when I backed up my home and root from another drive to new one using timeshift… There may have been some error, but after I successfully managed to make Arch work on the new drive I realised my zsh was breaking. That was when I moved back to bash. Maybe the timeshift issue is independent but I can’t think of any other reason.

I am seriously considering to either reinstall Arch, but all the programs from Spack installs are going to take ages to compile T_T. My other option is to learn to use the history command and sort and grep through that to find what I want :man_shrugging:

How about?

Step 1 - Log out of all terminals

Close all terminals and bash shell. Maybe be do it in a single user mode.

Step 2 - Remove all duplicates from bash histroy

I found this command

nl $HOME/.bash_history | sort -k 2  -k 1,1nr | uniq -f 1 | sort -n | cut -f 2 > /tmp/new_history
ls -l $HOME/.bash_history /tmp/new_history

Step 3 - Overwrite

cp -f /tmp/new_history $HOME/.bash_history 
chown $USER:$USER  $HOME/.bash_history  

Step 4 - Reboot




No. You misunderstand. The next terminal I open writes the contents of bashrc into bash_history so copying or overwriting or editing contents of bash_history is pointless. Thanks for your reply anyway :slight_smile:

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki