UPWORK/ODESK guru: Tips and Tricks for clients

skype: upwork.link , email: info@upwork.link

BASH: while read line <<< $variable

without comments

How can you read from variable with ‘while read line’? This can save you disk writes.
You can write:

while IFS= read -r line
    echo "$line"
done <<< "$the_list"

IFS= sets $IFS to the empty string (so it doesn't contain any characters at all). In this case, since there's only one field, its only effect is to prevent the removal of leading IFS-characters from the start of the line.


If you do not use the variable for anything else, you can even do without it:

while read line ; do
    echo $line
done <<( ... code ... )


You can just use

your_code | while read line;
    echo $line

if you don't mind the while loop executing in a subshell (any variables you modify won't be visible in the parent after the done).

Written by spectre

June 4th, 2013 at 8:56 pm

Posted in Bash,Tips and Tricks

Leave a Reply