Super safe bash history

Intro

Solution

function make_bash_history_safe() {
history -a # Save current history in memory to ~/.bash_history
history -c # Clear current history list in memory
# Comment all uncommented commands in ~/.bash_history with sed
sed -i -e 's/^\([^#]\)/# \1/g' ~/.bash_history
history -r # Reload history in memory from ~/.bash_history file
}
# Execute make_bash_history_safe after each command execution
PROMPT_COMMAND="make_bash_history_safe; "

Result

A screencast of safe history after each command execution
history -a; history -c; sed -i -e 's/^\([^#]\)/# \1/g'  ~/.bash_history ; history -r;

Slight Pitfall

<Ctrl-a> ; Go to beginning of line
<Ctrl-d> ; Delete first character on line, `#` in this case
<Return> ; Run the command
function remove_comment() {
READLINE_LINE=`echo ${READLINE_LINE} | sed -E -e "s/^[# ]+//g"`
}
bind -x '"\C-x\C-x": remove_comment'

Outro

--

--

--

Linux Geek and Anime Freak

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Budget Serverless Static Hosting In AWS

從 playground 調色去背圖片

Amature Bioinformatics —How to Write an Algorithm for Finding the Most Frequent Kmers within DNA…

Create a Distributed Database with High Availability with Apache ShardingSphere

Managing Cloud Services via Kubernetes CRDs

Distributed Ledger Platform: Powering Modern Payment Applications

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ayush Goyal

Ayush Goyal

Linux Geek and Anime Freak

More from Medium

Little Little Troubleshooting for Various Things

Linux Load Averages

Bash Basics and Navigating Directory Trees

Introduction to Unix Command Line