Bash: loop over all lines of a file (the right way)

less than 1 minute read

I have to admit that I’m a candiate for the Useless use of cat award sometimes.

I tend to use something like

for line in $(cat file); do echo $line; done;

. Yes, shame on me, but I promise improvement.

So the better way to to this at least in bash scripting is:

    while read line; do 
        echo $line
    done < file

For zsh there are further alternatives…