I have a function
foo_bar()
{
echo "foo_bar"
read -p "enter option" option
echo $option
}
option=$(foo_bar)
echo $option
It returns
enter option
and then in one line
foo_bar [option]
What I am expecting is the execution follows the order I write it..
i.e like
foo_bar
enter option
[option]
How do i do this in shell script
You are capturing the output of
foo_barin the variableoption. That output includes the text that foo_bar echos. (That is, you are assigning the stringfoo_bar\n$optionto the variableoption, and echo is replacing the newline with a space) However,read -pprints its prompt to stderr. If you want “foo_bar” to print to the same place that read prints its prompt, do so explicitly: