Zubair Zubair - 2 years ago 104
Bash Question

In a Linux shell how can I process each line of a multiline string?

While in a Linux shell I have a string which has the following contents:


and I want to pass each item as an argument to another function. How can I do this?

Answer Source

Use this (it is loop of reading each line from file file)

cat file | while read a; do echo $a; done

where the echo $a is whatever you want to do with current line.

UPDATE: from commentators (thanks!)

If you have no file with multiline, but have a variable, use

echo "$variable" | while read a; do echo $a; done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download