Confirm Before Running Another Command
Ever wanted a command that asks for confirmation before executing another command? No? Well, me neither! Here’s a command that will ask for your confirmation before running the next command:
How it Works
confirm() {
echo -n "Do you want to run $*? [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
"$@"
else
echo "Cancelled by user"
fi
}
How to Use
root@root:~# confirm echo "Hello world!"
Do you want to run echo Hello world!? [N/y] y
Hello world!
Conclusion
This command is a simple but useful tool for having a bit more control over what commands are executed on your system. By asking for confirmation before running a command, you can avoid mistakes and ensure that you are intentionally running the commands you intend to.
Frequently Asked Questions
Q: How do I use this command?
A: Simply call the confirm command followed by the command you want to run, like confirm echo "Hello world!".
Q: What if I type something other than ‘y’ or ‘n’?
A: The command will simply ignore your input and ask again.
Q: Can I customize the prompt?
A: Yes, you can modify the prompt by changing the echo -n "Do you want to run $*? [N/y] " line. For example, you could change it to echo -n "Are you sure you want to run $*? [Y/n] " if you prefer a different confirmation prompt.

