I've been writing some script to configure some firewall and came up with this way of asking a question in Bash. I hope it helps as an example. Feel free to contribute your own:
#!/usr/bin/env bash
ask() {
local query=$1
if ( echo $query | grep -qi password ); then
read -s -p "$query: " answer
echo
else
read -p "$query: " answer
fi
return 0
}
ask 'What is your name?'
echo
echo "Hello, $answer"
ask 'Please, give me your password'
echo
echo "Access granted"
As you can devise from the script, "$answer" is a global; which will get overwritten if you use ask() again. In case you want to make several questions, just reassign the var to another one:
ask 'What is your first name?'
firstname=${answer^}
ask 'What is your last name?'
lastname=${answer^}
echo
echo "Nice to meet you, $firstname $lastname!"
So, this gives you an idea. I'd check this article to learn some defensive Bash programming; which I am just starting to absorb: http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/