Bash: pipe |

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Commands used with a pipe | are run in a sub-shell, with the same pitfalls as using parenthesis might have:
http://linux.overshoot.tv/wiki/bash_parenthesis

This will not give the expected result, which is to abort at the first wrong line:

#!/bin/bash

cat file.txt | while read line
do
    if [ there_is_something_wrong_with_the_line ]; then
       exit 1
    fi
    do_something
done

Use input redirection instead:

#!/bin/bash

while read -r line
do
    if [ there_is_something_wrong_with_the_line ]; then
       exit 1
    fi
    do_something
done < file.txt