Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe a list of strings to for loop

Tags:

bash

How do I pass a list to for in bash?

I tried

echo "some
different
lines
" | for i ; do 
  echo do something with $i; 
done

but that doesn't work. I also tried to find an explanation with man but there is no man for

EDIT:

I know, I could use while instead, but I think I once saw a solution with for where they didn't define the variable but could use it inside the loop

like image 715
rubo77 Avatar asked Apr 10 '15 11:04

rubo77


1 Answers

for iterates over a list of words, like this:

for i in word1 word2 word3; do echo "$i"; done

use a while read loop to iterate over lines:

echo "some
different
lines" | while read -r line; do echo "$line"; done

Here is some useful reading on reading lines in bash.

like image 167
Tom Fenech Avatar answered Sep 28 '22 08:09

Tom Fenech