Created
October 12, 2025 01:34
-
-
Save markizano/bc9158eb6731a93a0aee915ec6ebd922 to your computer and use it in GitHub Desktop.
`histlog`: Maintain bash history across sessions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # @date: 2021-04-25 | |
| # @author: Markizano Draconus | |
| # @description: I got sick and tired of my bash history dissapearing despite using | |
| # HISTFILESIZE=-1, which disables truncating $HISTFILE | |
| # This script created to export history commands to a sub-directory in my ~/.bash_history.d/ | |
| # folder to avoid my commands from getting truncated from my $HISTFILE. | |
| # @usage: export PROMPT_COMMAND='history -a; histlog $(history 1)' | |
| # Acquire the history files and directory... | |
| export HISTLOG=${HISTLOG:-~/.bash_history.d} | |
| export HISTLOGFILE="$HISTLOG/`date +%Y-%m`.bash_history" | |
| # The last history item will be presented as the first argument. | |
| # Since history operations are not available from scripts. | |
| HISTLASTITEM="$1" | |
| # Make sure directory exists. | |
| test -d "$HISTLOG" || { | |
| mkdir -p "$HISTLOG" | |
| # ... and is owned by me. | |
| test "$UID" -eq 0 && chown $SUDO_UID:$SUDO_GID "$HISTLOG" | |
| } | |
| # Strip the leading spaces, index number so we get $date: $command | |
| histItem=`echo "$HISTLASTITEM" | perl -pe 's/^\s*\d+\s+//'` | |
| # If I am just pressing [Enter] and no command was run, | |
| test -e "$HISTLOGFILE" && { | |
| # Don't append the previous command again to the log. | |
| lastItem=`tail -n1 "$HISTLOGFILE"` | |
| test "$histItem" == "$lastItem" && exit 0 | |
| } | |
| # Append the history command. | |
| echo "$histItem" >> "$HISTLOGFILE" | |
| # Make sure the history file is owned by me. | |
| test "$UID" -eq 0 && chown $SUDO_UID:$SUDO_GID "$HISTLOGFILE" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment