A function I have been using in my bash scripts allows me to collect user input in a very simple way.
port=$(input_user "what port for server?" 8080)
Which will prompt the user for a text input, but also have the option for a default answer.
The function looks like this . . .
# input decision for user, useful for assigning variiable values
# usage: prompt_user <prompt message> [fallback value*]
# * uses fallback/default value if no input recieved
# example:
# name=$(input_user "what is your name?")
# port=$(input_user "what port for server?" 8080)
input_user() {
local input=
# set text prompt value
local prompt="${1:-value}"
# set default value
local default="$2"
[ -z "$default" ] || prompt+=" [$default]"
# convert escape sequences in prompt to ansi codes
prompt="$(echo -en "$prompt : ")"
while [ -z "$input" ]; do
if [ -t 0 ]; then
# user input
read -p "$prompt" input </dev/tty
else
# piped input
read input
fi
[[ -n "$default" && -z "$input" ]] && input="$default"
[ -z "$input" ] && echo -en "invalid input"
done
echo "$input"
}
You can even read back that value into a variable which you can check. Which allows you to do more complex interactions from your bash commands, set options and allow them to be more robust. Here is the same function being used to read a simple choice...
# Asks the user "yes" or "no"
delete_check() {
ANSWER=$(input_user "Are you sure you want to Delete this?" no)
if [ "$ANSWER" == "yes" ]; then
echo -en "deleting this . . .";
elif [ "$ANSWER" == "no" ]; then
echo -en "Exiting without Deleting . . .";
else
echo -en "Invalid Input, Answer 'yes' or 'no'";
fi
return 0;
}
Using the read -p
command above means you want to execute read
using the -p
option, which stands for prompt.
-p (prompt) - Display prompt on standard error, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
If checks the return string of the input function, using a string comparison. But there are many other things you can do with responses.