bash loop though multi line variable


I have a multi line variable in my bash script and wanted to iterate through each line of a variable.

To do this task I am using while loop.

Following is my variable:


As I mentioned to loop through my multi line variable(var) I am using while as shown below:

while read -r line
  echo "line: $line"
done <<< "$var"

Just carefully observer last line in above code, here I am using three less than symbols(<<<) and variable in double quotes.

line: a
line: b
line: c
line: d
line: e

Above example only works with bash.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s