Tuesday, November 2, 2021

[SOLVED] Read line by line in Bash script

Issue

I want to do the following: Read a file line by line and use the line as a parameter.

FILE="cat test"
echo "$FILE" | \
while read CMD; do
echo $CMD
done

But when I do echo $CMD, it just prints cat test.


Solution

What you have is piping the text "cat test" into the loop.

You just want:

cat test | \
while read CMD; do
    echo $CMD
done


Answered By - Oliver Charlesworth