Skip to content

Instantly share code, notes, and snippets.

@thomaslaurenson
Created May 14, 2018 06:14
Show Gist options
  • Save thomaslaurenson/ae72d4b4ec683f5a1850d42338a9a4ab to your computer and use it in GitHub Desktop.
Save thomaslaurenson/ae72d4b4ec683f5a1850d42338a9a4ab to your computer and use it in GitHub Desktop.
# Custom history configuration
# Run script using:
# chmod u+x better_history.sh
# sudo su
# ./better_history.sh
echo ">>> Starting"
echo ">>> Loading configuration into /etc/bash.bashrc"
echo "HISTTIMEFORMAT='%F %T '" >> /etc/bash.bashrc
echo 'HISTFILESIZE=-1' >> /etc/bash.bashrc
echo 'HISTSIZE=-1' >> /etc/bash.bashrc
echo 'HISTCONTROL=ignoredups' >> /etc/bash.bashrc
echo 'HISTIGNORE=?:??' >> /etc/bash.bashrc
echo '# append to history, dont overwrite it' >> /etc/bash.bashrc
echo 'shopt -s histappend' >> /etc/bash.bashrc
echo '# attempt to save all lines of a multiple-line command in the same history entry' >> /etc/bash.bashrc
echo 'shopt -s cmdhist' >> /etc/bash.bashrc
echo '# save multi-line commands to the history with embedded newlines' >> /etc/bash.bashrc
echo 'shopt -s lithist' >> /etc/bash.bashrc
echo '# After each command, append to the history file and reread it' >> /etc/bash.bashrc
echo 'export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$"\n"}history -a; history -c; history -r"' >> /etc/bash.bashrc
# Reload BASH for settings to take effect
echo ">>> Reloading BASH"
exec "$BASH"
echo ">>> Finished. Exiting."
@thomaslaurenson
Copy link
Author

A simple script to enforce better history in Linux systems. Should preserve history when multiple different connections to a system are established. For example, multiple SSH or PuTTY sessions. Also, appends date/time stamps to each history entry.

@ggrandes
Copy link

ggrandes commented Nov 4, 2019

@shaigluskin
Copy link

@thomaslaurenson, I love this script. 2 things:

  1. When switching between open bash sessions, the user must hit the enter key in order for that session to have access to the recent history of the other open sessions. For folks who fully understand how PROMPT_COMMAND works, that is probably self-evident. But for others, like me, I thought that part of the script wasn't working because I would switch to another open bash session, hit the up arrow immediately, thinking I would have access to a command I had just used in a different session, and was disappointed. It took me a while to realize what was going on. So maybe including a note along the lines of:
    # Caveat: when switching between bash sessions user must hit enter key to update bash history from other sessions
  2. On line 21 I would suggest single quoting "\n" so that it becomes '\n'. I don't think it is causing a problem in this instance. But when I was trying figure out why sessions weren't updating history I was distracted by this and thought that it might be causing the problem.

Again, thanks for the great blog post and script.

@seamusdemora
Copy link

Thanks for your blog post on bash history, and this script.

I'm still evaluating, but as of now, I wonder if your PROMPT_COMMAND is, uh... "a universally good thing"?

PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$"\n"}history -a; history -c; history -r"

Here's why: At times, I keep 2 or 3 SSH sessions open to hosts where I use bash as the interactive shell. I use these sessions for different purposes; e.g. one session may be used for script development, while another is used for support... man whatever & that sort of thing. Working this way, a unique session history has advantages. For me, PROMPT_COMMAND='history -a' is all I really want while I am working - I don't need a "merged" history from other sessions. This of course is personal; it depends on one's use case or workflow.

So that's it - just a comment with another point of view.

@johanrhodin
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment