The Wayback Machine - https://web.archive.org/web/20240711153906/https://www.geeksforgeeks.org/bash-scripting-how-to-read-a-file-line-by-line/
Open In App

Bash Scripting – How to read a file line by line

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to read a file line by line in Bash scripting.

There might be instances where you want to read the contents of a file line by line using a BASH script. In this section, we will look at different ways to do just that. We will use BASH commands and tools to achieve that result. We make use of the read and cat commands, for loops, while loops, etc to read from the file and iterate over the file line by line with a few lines of script in BASH.

Method 1: Using read command and while loop

We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters. 

#!usr/bin/env bash

file="temp.txt"

while read -r line; do
    echo -e "$line\n"
done <$file 

In the following example, we can see that we have an iteration over a file line by line and we store the contents of a single line in the variable “line“.  The file name is stored in the variable file and this can be customized as per requirements. You can run the script using the following command (Here filereader.sh can be any name you give to your script).

bash filereader.sh

We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.

The file can be also inputted by parsing it as a positional parameter. 

bash filereader.sh filename

The filename can be any file you want to read the contents of. You need to change the script where the file variable has been declared.

file=$1

This will take the first parameter after the script name as the filename to be used in the script. Hence we can make the script dynamically change the file as per the input given.

Method 2: Using cat and for loop

From this approach, we can read the contents of a file using the cat command and the for a loop. We use the same approach for a loop as in the while loop but we store the contents of the file in a variable using the cat command. The for loop iterates over the lines in the cat command’s output one by one until it reaches EOF or the end of the file.

Here as well we can incorporate the positional parameter as a filename. We need to replace the filename with $1 in the script.

file=$(cat $1)

This will take the file from the command line argument and parse it to the cat command to further process the script. 

#!usr/bin/env bash

file=$(cat temp.txt)

for line in $file
do
    echo -e "$line\n"
done

Thus, from the above examples, we were able to read the contents of a file line by line in a BASH script.



Similar Reads

Bash Scripting - Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to a
10 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we print or echo some text on screen with the help of a t
2 min read
Bash Scripting - Working of Bash Scripting
Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and ot
6 min read
Bash Scripting - Bash Read Password without Echoing back
Generally, you can see on the login page, whenever we type a password either it show dot (â€Ē) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And in some cases, a dot or asterisk also may not appe
3 min read
Bash Scripting - Write Output of Bash Command into Log File
Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file. Let see how to write the output of the bash command into a log file.
4 min read
Bash Scripting - Difference between Zsh and Bash
A shell is an environment in which various commands can be executed, it provides an interface between the user and the UNIX system. Basically, a shell is a command-line interpreter which interprets the commands given by the user, it can also read the combination of such commands which is known as a shell script. The shell provides us with an enviro
2 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally three options:-n: It does not print the trailing n
2 min read
Bash Scripting - How to check If File Exists
In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a common ( regular ) file.-d: it returns True if direct
6 min read
Bash Scripting - File Extension
Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file name. For example, in the file name "file.txt", the
6 min read
Leap Year Program with Bash Shell Scripting in Windows
Accurately the Earth has 365.24 days per year, to correct the approximation and keep track of the Solar Calendar the idea of leap year has been introduced to Gregorian Calendar. A leap year comes once every four years. In contrast, it has 366 days with regular years, having 365 days. Mathematical approach to check:Every year is a Leap year if it is
1 min read
Article Tags :