#!/bin/bash confirmation() { # Receive confirmation from user as y, Y, n, or N # returns 0 when answer is yes and 1 when answer is no # # Arguments: # message # - The confirmation prompt sent to the user, for example: # Would you like to overwrite foobar.txt (y/N)? # # Usage: # confirmation "Some prompt" # - Sends "Some prompt" to the user and gets their input # # POSIX Compliant: # Yes # local message message="${1}" local choice while true; do read -p "${message} " -n 1 -r choice case "$choice" in y | Y) echo "" return 0 ;; n | N) echo "" return 1 ;; *) echo -e "\nInput must be either y, Y, n, or N" ;; esac done }