Skip to content

Instantly share code, notes, and snippets.

@n0nick
Last active August 29, 2015 14:20
Show Gist options
  • Save n0nick/a65f6696b95e44dbfd1d to your computer and use it in GitHub Desktop.
Save n0nick/a65f6696b95e44dbfd1d to your computer and use it in GitHub Desktop.
General purpose bash ask prompt
# Usage:
# ask "What's up?" answer "ok"
# echo $answer
ask() {
local question=$1
local default=$3
local resultvar=$2
echo -n "$question "
if [ "$default" ]; then
echo -n "[$default] "
fi
read reply
echo
reply="${reply:-$default}"
reply=$(echo $reply | sed "s/'/'\"'\"'/g")
eval $resultvar="'$reply'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment