Bash – Assign specific line from a variable to another variable

awkbashcommand lineshellvariable

I would like to take a specific line from a variable to another variable.
I tried this but it doesn't work:

c="1.apple
2.banna
3.peach"

read "Please choose fruit [1-3]:" t

a=$c | awk "NR==$t"
echo "You choose: $a"

What is my mistake?

Best Answer

Use Here String redirection <<< together with Command Substitution $() and don't forget to put double quotes around your variables:

a=$(awk "NR==$t" <<< "$c")
Related Question