Created
May 14, 2012 19:43
-
-
Save jsocol/2696070 to your computer and use it in GitHub Desktop.
Bash prompt function
This file contains hidden or 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
_prompt() { | |
local _prompt=$1 | |
local _out=$2 | |
local _default=$3 | |
local _val='' | |
if [ $_default ]; then | |
_prompt="${_prompt} [${_default}]" | |
fi | |
read -p "$_prompt " _val | |
if [ $_val ]; then | |
eval $_out="'$_val'" | |
else | |
eval $_out="'$_default'" | |
fi | |
} | |
# Usage: _prompt "Question?" TARGET_VARNAME ["default"] | |
# For example: | |
_prompt "What is your name?" NAME "Steve" | |
# What is your name? [Steve] _ | |
echo $NAME # "Steve" or whatever the user entered. | |
_prompt "Pick a number." NUMBER | |
# Pick a number. _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment