Zubair Zubair - 6 months ago 12
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:

cat
dog
bird


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

Answer

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
Comments