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.

One comment

  1. Just to let you know, both of those examples contain what is known as “useless uses of cat”. It shows bad coding practice for bash scripts, opening up more processes than are necessary. Google ‘useless uses of cat’ for more information on this common faux-pas.

    The following does the same, without the waste of a cat:


    while read line; do
    echo $line | rev
    done < file.txt

    #==========


    ls -1 | while read line
    do
    sed 's/foo/bar/g' < $line
    done

Leave a Reply

Your email address will not be published. Required fields are marked *