Skip to content

Instantly share code, notes, and snippets.

@tomasaschan
Created March 1, 2018 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasaschan/9090ad5dc02669e190340850233a27ce to your computer and use it in GitHub Desktop.
Save tomasaschan/9090ad5dc02669e190340850233a27ce to your computer and use it in GitHub Desktop.
Bash snippet for a confirmation prompt
function confirm() {
confirmed=
read -p "$1 (y/n): " choice
while [[ -z $confirmed ]]; do
case "$choice" in
y|Y)
confirmed=true
;;
n|N)
confirmed=false
;;
*)
read -p "Invalid choice; answer y for yes or n for no (y/n): " choice
;;
esac
done
echo "$confirmed"
}
if [ $(confirm 'Are you sure?') = true ]; then
echo 'OK, I'll do it!'
else
echo 'OK, nevermind then.'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment