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