Bash script – read stdin line by line

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:

  1. cat file.txt | while read line
  2. do
  3. echo $line | rev
  4. done

Read the output of an ls and cat each file through sed:

  1. ls -1 | while read line
  2. do
  3. cat $line | sed 's/foo/bar/g'
  4. done

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.