Last Updated: September 09, 2019
·
1.848K
· Rilke Petrosky (XenoMuta)

Safety tips for dangerous commands in bash

If you are a command line interface addict (like me), then you might as well have accidentally run commands you thought weren't there in your bash history ( like rm -fr *, reboot, dd if=/dev/urandom of=/dev/sdb..., you get the point... ), or maybe expose some password entered from command line.

Bash provides the HISTCONTROL variable to allow you to ignore commands starting with a space. Make sure it contains the ignorespace option and add that to your ~/.bashrc ( in case you don't already have it )
echo HISTFILE=ignoredups:ignorespace >> ~/.bashrc

Well, then, make sure you start commands you don't want in your bash history with a space.

If you DO need to keep this command in history, but would love to have it done carefully, this one liner can help you out with a noisy confirmation prompt:

danger() { read -p "$(echo -ne "\x1b[01;41;37m ARE YOU SURE [y/n]?? ")" -N 1 CONFIRM; echo -e "\x1b[0m"; if [ "${CONFIRM}" == "y" ]; then $*; fi; }

3 Responses
Add your response

You mean, you accidentally invoke those commands when you Ctrl+R or something?
Or all team members surrender their bash history files when a production server goes down? :)

over 1 year ago ·

Interesting tip on the same topic: Preventing accidental system halts and reboots

over 1 year ago ·

Molly-guard... hum... In fact, I'll give that one out a try, because SSH is a completely different situation in which my pro-tip is helpless.

over 1 year ago ·