This is a really useful bit of code which I use almost daily to speed up routine tasks. You can pipe text, split into multiple lines, into a while loop and read through them line by line. This is how you do it:
Read a file line by line and echo (reversed) the line:
- cat file.txt | while read line
- echo $line | rev
Read the output of an ls and cat each file through sed:
- ls -1 | while read line
- cat $line | sed 's/foo/bar/g'
You’ll see that you ‘while read XXXX’. $XXXX then becomes the variable that references that line. So in the above example ‘while read line’ produces $line. Naturally, there are better ways to do the above tasks but it hopefully gives you an idea of the power of a while loop in bash.