How to do a while loop with a string redirected into it

Im trying to loop though a string with HTTP links inside and newlines, I want to loop over a line at a time.

At the moment I have

echo -e "$HTTP_LINKS" | while read HTTP_S_LINK ; do

But this way I don't have access to the TEST_STRING out side the loop, which is what I want. I'm using the while loop so that it will loop though each newline in $HTTP_LINKS and not just the words in the string. (I don't want to use a for loop with IFS set to \n)

I thought maybe I could just do something like this

while read HTTP_S_LINKS
done < $HTTP_LINKS

But of course this doesn't work as $HTTP_LINKS contains a string and not a link to a file.


You had the right idea with your 2nd snipit but you need to use 'Here Strings' via the <<< syntax. You cant access $TEST_STRING outside of your first snipit because the pipe creates a sub-shell; using the here-string does not. Also, make sure you quote "$HTTP_LINKS" otherwise you'll lose the newlines.


HTTP_LINKS=$(echo -e "\n")


while read url; 
done <<<"$HTTP_LINKS"


If you initialize and export the TEST_STRING variable outside the loop you should have access to it after the loop.

Need Your Help

Django select how to remember chosen value?

python html django select post

I have a select. On item change I reload a page, but after reloading I have a blank item chosen. How to make remember chosen item?

Need a ruby solution for executing a method in separate process

ruby process asynchronous eventmachine workling

I am implementing a poller service whose interface looks like this.