Last active
August 29, 2015 14:20
-
-
Save n0nick/a65f6696b95e44dbfd1d to your computer and use it in GitHub Desktop.
General purpose bash ask prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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