Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Linux by (18.4k points)

In my Linux shell I have the string that has the following contents:

cat

dog

bird

I wanted to pass each item as the argument to a different function. How can I do this?

1 Answer

0 votes
by (36.8k points)
edited by

Use this 

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

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

UPDATE: 

If you don't have a file with the multiple lines but have a variable with multiple lines, then use the below code:

echo "$variable" | while read -r a; do echo $a; done

To know about Linux join the Linux training

Do check the below video 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...