Alex Rivera | Logout

How to use the read command in Bash?

Asked 2011-10-06T14:45:25.410
63

When I try to use the read command in Bash like this:

echo hello | read str
echo $str

Nothing echoed, while I think str should contain the string hello. Can anybody please help me understand this behavior?

Edit
Report

1 Answer

8

Typical usage might look like:

i=0
echo -e "hello1\nhello2\nhello3" | while read str ; do
    echo "$((++i)): $str"
done

and output

1: hello1
2: hello2
3: hello3
answered 2011-10-06T15:34:57.570

Your Answer