I’m have a function wrapper for read
command. I’m trying to automate most of input asking in my script by a function. Below is non-working code.
ask_input() {
_question="$1"
_thevar="$2"
_finalvar=$(eval $(echo $_thevar))
read -ep "${_question}: " "$_thevar"
printf "%sn" "$_question" "$_thevar" "$_finalvar"
}
Basically what I’m hoping is when I execute following.
ask_input "Do you like apple (yes/no)" the_answer
If an user type yes
, each variable will contain following (w/o quotes of course, it was just for easier readability).
$_question --> "Do you like apple (yes/no)"
$_thevar --> "the_answer"
$_finalvar --> "yes"
The eval
command is my attempt to solve the problem, but I have not found the actual solution to this.