How To Read File Line by Line in Bash
This post will guide you how to write an Bash shell script to read a given file line by line in your Linux system.
You can use while read line construction to read a file line by line in bash shell.
For example, if you have a list of company name in a file named name.txt
, and you want to read it line by line in a bash shell script, as followss:
$ cat name.txt [devops@mydevops ~]$ cat name.txt google bing yahoo facebook oracle
then you can create a bash script named readFile.sh
, and add the following line into the file.
#!/bin/bash FILE=$1 while read LINE; do echo "line: $LINE" done < $FILE
save and close the file. and you still need to make it executable with the following command:
$ sudo chmod u+x readFile.sh
Executing this script with the following command:
$ ./readFile.sh name.txt
Outputs:
[devops@mydevops ~]$ ./readFile.sh name.txt line: google line: bing line: yahoo line: facebook line: oracle
Conclusion
You should know that how to read file line by line in a bash shell script in your Linux system.