Record every bash/zsh command


I can never remember exactly what I typed to get things to work, so I made this little snippet to save every command I’ve typed, along with the current directory and timestamp.

precmd() {
    if [ "$(id -u)" -ne 0 ]
    then
        echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(fc -ln -1)" >> ~/bash/bash-history-$(date "+%Y-%m-%d").log;

    fi
}

One note of caution: don’t use any plain-text passwords/keys/tokens/etc in your commands: they will get logged! Generally speaking, this a good idea anyway, since these are visible in ps while the command runs.

Date: 2022-11-01, Updated: 2023-01-21

Tags:  bash

Share: