What does HackerNews think of bash-preexec?
⚡ preexec and precmd functions for Bash just like Zsh.
- Add both files in this gist to $BASH_HOME.
https://gist.github.com/chmaynard/dbcaed11534dc54bdc90856d18...
- Install .bash-preexec.sh in $HOME (see https://github.com/rcaloras/bash-preexec).
- Append this command to your shell startup file (e.g. $BASH_HOME/.bashrc):
source $BASH_HOME/bash_history.sh
- Open a new bash window. You should see the message "bash-preexec is loaded."Enjoy! Please share your feedback in the gist comments section.
(Memo to self: Package this up in a repo with a proper README.)
preexec_custom_history() {
echo "$HOSTNAME $$ $(date "+%Y-%m-%dT%H:%M:%S%z") $1" >> "~/.fullhistory"
}
preexec_functions+=(preexec_custom_history)
I work on a lot of shared-filesystem computers, and so it's useful to be able to filter for commands ~year ago when I know what computer I was on when I ran it. I have a fzf binding to search through this with ctrl-r, and it's otherwise easily greppable.When I was still using bash, I had this same thing using https://github.com/rcaloras/bash-preexec .
echo "$HOSTNAME $$ $(date "+%Y-%m-%dT%H:%M:%S%z") $*" >> ~/.fullhistory
It's attached to the preexec hook of https://github.com/rcaloras/bash-preexec, so is run before every command. This means that everything goes into one easily-greppable file, but is still separable by PID/host machine - since my work has me walking around a large facility, often I'll remember where I was when I did something but not exactly when, so can narrow down by machine.Some of the features of my solution are that it creates a sub folder in the user's home directory called ~/.bash_history and underneath this it will create sub folders for each month (YYYY-MM) and under each of those sub folders will reside a daily audit log file of all the bash command history (YYYY-MM-DD). The audit script logs both login and log outs, as well as each command executed in bash.
---
[0] https://github.com/onelittlehope/bash-prompt [1] https://github.com/rcaloras/bash-preexec
I use bash-preexec [1] to hook on the even just before executing a command. I'm kind of nervous if my string quoting is safe though.