Skip to content

Instantly share code, notes, and snippets.

@tinytengu
Created June 25, 2021 18:09
Show Gist options
  • Save tinytengu/9c2d9ce5d2cfa54529625cd3849c3db1 to your computer and use it in GitHub Desktop.
Save tinytengu/9c2d9ce5d2cfa54529625cd3849c3db1 to your computer and use it in GitHub Desktop.
Bash Yes/Now prompt implementation with default answer
prompt_yn() {
def=$2
# Normalize default answer and prompt
case $def in
[yY]) def="y"; read -r -p "${1} [Y/n] " input;;
[nN]) def="n"; read -r -p "${1} [y/N] " input;;
esac
# Echo 1 if yes else 0
case $input in
"")
if [ $def = "y" ]; then
echo 1
else
echo 0
fi
;;
[yY][eE][sS]|[yY]) echo 1;;
[nN][oO]|[nN]|*) echo 0;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment