Well, here's my version of confirm
, modified from James' one:
function confirm() {
local response
local msg="${1:-Are you sure?} [y/N] "; shift
read -r $* -p "$msg" response || echo
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
function confirm() {
local response msg="${1:-Are you sure} (y/[n])? "; shift
read -r $* -p "$msg" response || echo
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
These changes are:
- use
local
to prevent variable names from colliding read
use$2 $3 ...
to control its action, so you may use-n
and-t
- if
read
exits unsuccessfully,echo
a line feed for beauty - my
Git on Windows
only hasbash-3.1
and has notrue
orfalse
, so usereturn
instead. Of course, this is also compatible with bash-4.4 (the current one in Git for Windows). - use IPython-style "(y/[n])" to clearly indicate that "n" is the default.