In this particular case, I\'d like to add a confirm in Bash for
Are you sure? [Y/n]
for Mercurial\'s hg push ssh://username@www.example.com//some
Well, here's my version of confirm
, modified from James' one:
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:
local
to prevent variable names from collidingread
use $2 $3 ...
to control its action, so you may use -n
and -t
read
exits unsuccessfully, echo
a line feed for beautyGit on Windows
only has bash-3.1
and has no true
or false
, so use return
instead. Of course, this is also compatible with bash-4.4 (the current one in Git for Windows).