Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks. Not what I was looking for, but these are very interesting alternatives. – Karthik Aug 10 '10 at 3:03

I am guessing you are trying to create a command inside a variable and then run it by specifying the variable? Then don't. run the command as it is.

share|improve this answer
    
Actually, I'm parsing really large numbers (1000+ digits). I wanted to store it into a variable because it got unwieldy after a while. – Karthik Aug 10 '10 at 2:27

Nope. If you prefer the behaviour of a different shell, switch to that shell. Market forces. :)

share|improve this answer
read quot < <(echo "Alexander the Grape")
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .