Skip to content

Instantly share code, notes, and snippets.

@Rondom
Created August 30, 2012 15:05
Show Gist options
  • Save Rondom/3530333 to your computer and use it in GitHub Desktop.
Save Rondom/3530333 to your computer and use it in GitHub Desktop.
ask_variable (bash-only)
#!/bin/bash
# 1. Text
# 2. variableName
# 3. additonal parameters for read
function ask_variable() {
askText="$1"
varName="$2"
eval default="\$$varName"
bashVersionMajor=$(echo ${BASH_VERSION:-3.0.0} | cut -d "." -f1)
if [ "$bashVersionMajor" -ge "4" ]; then
# use -i for bash-versions >= 4
read $3 -e -p "$askText > " -i "$default" "$varName"
else
# set to default when string is empty for older bashs
read $3 -e -p "$askText [$default]> " "$varName"
eval "$varName=\${$varName:-$default}"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment