What does HackerNews think of bash-preexec?

⚡ preexec and precmd functions for Bash just like Zsh.

Language: Shell

#21 in Bash
- Save a copy of your current .bash_history file. You can import its contents into the sqlite "commands" table later.

- 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.)

Or use the excellent bash-preexec plugin that atuin itself relies on to achieve this in a cleaner way: https://github.com/rcaloras/bash-preexec
I got burned a couple of times in the past with bash-history, and go a small step beyond this, even on zsh, and added to .zshrc:

    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 .

I do something similar, I gave up on trying to get the bash history to work properly and so just have this:

    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.
I implemented bash audit logging[0] by making use of Ryan Caloras's bash-prexec[1] project which provides a fairly robust and resilient way to implement ZSH's preexec and precmd functionality.

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

http://pastebin.com/zJkPW79C

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.

[1] https://github.com/rcaloras/bash-preexec