LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Looping using while read line, using a variable instead of a file. (https://www.linuxquestions.org/questions/programming-9/looping-using-while-read-line-using-a-variable-instead-of-a-file-834341/)

henrtm05 09-24-2010 04:40 PM

Looping using while read line, using a variable instead of a file.
 
I created the following.

x="top
middle
bottom"

I would like to make the following work.

while read line
do

echo $line

done

instead of importing a file I would like to use the variable $x I tried using pipes, but with no luck. My goal is to read one line at a time, but not have to export my data to another file, I would like to keep it all within one script.

Please help.

Kenhelm 09-24-2010 05:14 PM

Try a here string.
http://www.tldp.org/LDP/abs/html/x17471.html
Code:

while read line
do
  echo $line
done <<< "$x"


henrtm05 09-24-2010 05:26 PM

Quote:

Originally Posted by Kenhelm (Post 4108162)
Try a here string.
http://www.tldp.org/LDP/abs/html/x17471.html
Code:

while read line
do
  echo $line
done <<< "$x"


This worked for me, thank you I really appreciate the help.

grail 09-24-2010 11:49 PM

Please mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 05:04 AM.