Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • I like this one @ToniJarjour , I'm not exactly sure why but it seems more "sane" as you say. It's pretty clear what it's doing. I'm just trying to update it to handle different bash history's in different locations. I'm trying to implement a different bash history for each of my Tmux sessions. I source an "activate" script in the local environment every time I open a shell so I can easily export a different HISTFILE location in the local environment every time. Commented Jul 9, 2022 at 10:00
  • To work with different history files defined by $HISTFILE I think this update to the second line of the history clean function will do it. This should also be an improvement for those keeping their bash history somewhere other than the default: awk 'NR == FNR { a[$1]++; next; }; ++b[$0] == a[$0]' "$HISTFILE" "$HISTFILE" > "$bash_history_file" Commented Jul 9, 2022 at 10:34
  • Nope. Screws up the bash history, I think because you may have not considered time stamps, but after removing time stamps only records "exit" in the history. Not working. Commented Jul 9, 2022 at 11:24
  • 1
    Ok, I fixed it by changing your awk line to: tail -r $HISTFILE | cat -n | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- | tail -r > $bash_history_file, where $HISTFILE can be used instead of $HOME/.bash_history. I got the line to eliminate duplicates while retaining order from unix.stackexchange.com/questions/194780/… and just reversed it fore and aft in order to preserve the most recent occurrence of duplicates instead of the first occurrence. Now works for me. Commented Jul 9, 2022 at 12:36
  • Bash doesnt have tail -r, but tac works. tac $HISTFILE | cat -n | sort -k2 -k1n | uniq -f1 | sort -nk1,1 | cut -f2- | tac Commented Oct 12, 2022 at 22:58