How to read file line by line in Bash script

29/12/2020
How would you write a Bash script that can process a text file one line at a time.  First you need a syntax and approach to read the file line by line. The methods for this approach are shown in this tutorial.

Suppose, you have a file named company.txt which contents the company names. This file contains the following content.

Company.txt
Samsung
Nokia
LG
Symphony
iphone

Example -1: Reading file content from command line

Suppose, you want to read the file, company.txt, line by line from the command line without ‘cat’ command. Run the following command to do the task. while loop will read each line from the file company.txt in each step and store the content of the line in $line variable which will be printed later.

$ while read line; do echo $line; done < company.txt

Example -2: Reading file content using script

Create a bash file and add the following code to read the content of a particular file. Here, an existing filename is stored in $filename variable and $n variable is used to keep the value of the line number of that file. Like previous example, while loop is used to read this file with line number.

#!/bin/bash
filename=‘company.txt’
n=1
while read line; do
# reading each line
echo "Line No. $n : $line"
n=$((n+1))
done < $filename

Run the following command to execute the script.

$ bash readfile1.sh

Run ‘cat’ command with company.txt file to display the original content of company.txt file.

$ cat company.txt

Example -3: Passing filename from the command line and reading the file

Create a bash file and add the following script. This script will take the filename from the command line argument. First argument value is read by the variable $1 which will contain the filename for reading. If the file exists in the current location then while loop will read the file line by line like previous example and print the file content.

#!/bin/bash
filename=$1
while read line; do
# reading each line
echo $line
done < $filename

Run the above script with employee.txt file as argument value. The output will show the content of employee.txt file by removing extra space. You can show the original content of employee.txt file by using ‘cat’ command.

$ bash readfile2.txt employee.txt
$ cat employee.txt

Example – 4: Reading file by omitting backslash escape

If you want to read each line of a file by omitting backslash escape then you have to use ‘-r’ option with read command in while loop.

#!/bin/bash
while read -r line; do
# Reading each line
echo $line
done < company2.txt

Create a file named company2.txt with backslash and run the following command to execute the script. The output will show the file content without any backslash.

$ bash readfile3.sh

You will need to read the file for many programming purposes. For example, you can search or match any particular content easily from any file by reading each line separately. So, it is an essential task for any programming. Some simple examples of reading file in bash script are shown in this tutorial. These will help you to get the idea of reading file content line by line using while loop in bash script and apply in your script more efficiently. For more information watch the video!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Parameter Expansion

The parameter is used in bash to store data. Different types of data can be stored in the parameter, such as integer, string,...
29/12/2020

Bash Export Command

If you’ve ever stood in front of a terminal, typed `declare -p` to see what is going on with your variables in bash, and...
29/12/2020

How to Check the OS version in Linux

For a regular Linux user and especially administrator, knowing the version of the OS they are running is very important....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Lý do tại sao bạn nên sử dụng proxy khi truy cập web đen
27/02/2023

Các lỗi thường gặp khi sử dụng proxy và cách khắc phục chúng.
27/02/2023

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.
27/02/2023

Các tiện ích và công cụ để quản lý proxy.
27/02/2023

Các cách để kiểm tra tốc độ và độ ổn định của proxy.
27/02/2023