The following are different ways of reading a txt file line by line in linux shell. To fun the following scripts, you should create a “test.txt” file under the same directory with your script.
Method 1
Use file names as parameter to the shell script.
readFile.sh
#!/bin/bash while read line do name=$line echo "Text read from file - $name" done < $1
Linux command to run:
chmod +x readFile.sh ./readFile.sh test.txt
Method 2
Put file name inside of shell script.
#/bin/bash file="test.txt" while IFS= read -r line do # display $line or do somthing with $line echo "$line" done <"$file"
Linux command to run:
. readFile2.sh