Created
July 30, 2025 12:07
-
-
Save xhd2015/cbf9024d76b5945078d002b545f16e0b to your computer and use it in GitHub Desktop.
Bash history recording and logging
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
| After asked grok3 lots of options, the only one that works for me: | |
| ```sh | |
| # log and append history while preserving $_ | |
| append_log_history() { | |
| # Save the current $_ | |
| local saved_underscore="$_" | |
| echo -n '' > /tmp/history$$ | |
| HISTFILE=/tmp/history$$ history -a | |
| cat /tmp/history$$ >> "$BASH_HISTORY_BACKUP_FILE" | |
| cat /tmp/history$$ >> "$HISTFILE" | |
| history -n | |
| # Restore $_ | |
| export _="$saved_underscore" | |
| } | |
| shopt -s histappend | |
| PROMPT_COMMAND="append_log_history; $PROMPT_COMMAND" | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment