Accessing the output of a Bash pipe with 'read'
I'm trying to pipe some data from a Bash pipe into a Bash variable using the read command, like this:
$ echo "Alexander the Grape" | read quot
$ echo $quot
$
But quot is empty. Some Googling revealed that this is not a bug; it's an intended feature of Bash. (Section E5 in the FAQ.)
But when I tried the same thing in zsh, it worked. (Ditto for ksh.) Is there any way to make this work in Bash? I really don't want to have to type:
$ quot=$(echo "Alexander the Grape")
Especially for long commands.
Solution 1:
For additional reading on this subject, see BashFAQ/024.
There are a bunch of ways to do variable assignments in Bash:
var=$(command)
read var <<< $(command)
read var <<< EOF
$(command)
EOF
printf -v var "%s" $(command)
etc.