Different Ways to Read File in Bash Script Using While Loop


Reading files is an essential aspect of shell scripting in the Bash environment. In Bash, reading files can be achieved using different techniques, and one of the most popular and versatile methods is using the while loop. The while loop in Bash is a powerful construct that allows us to iterate through a file line by line, making it easier to manipulate and process data. In this article, we will discuss various ways to read a file using while loops in Bash scripting.

Before diving into the different techniques of reading files in Bash, let us understand the syntax and functionality of the while loop.

Understanding the While Loop in Bash

The while loop in Bash follows a specific syntax, as shown below −

while [condition]
do
   # code to execute
done

The loop runs repeatedly until the condition returns false. The code within the loop executes until the condition returns false.

In Bash, we can use the while loop to read files using different methods, including using the read command, I/O redirection, and cat command.

Reading a File Using the read Command

The read command is a Bash built-in command used to read input from a file or the standard input (STDIN). We can use the read command to read a file in Bash by iterating through the file's content line by line using a while loop.

Here is an example of how to read a file using the read command in Bash −

#!/bin/bash

filename="sample.txt"

while read line
do
   echo $line
done < "$filename"

In the above example, we first define the file's name as filename. We then use the while loop to iterate through the file's content line by line. The read command reads each line of the file and assigns it to the line variable. We then use the echo command to print the line to the standard output. Lastly, we use I/O redirection to redirect the file's content to the read command.

Reading a File Using I/O Redirection

Another way to read a file in Bash is by using I/O redirection. I/O redirection is a technique used to redirect input or output from a file to a command or vice versa. We can use the while loop with I/O redirection to read a file in Bash.

Here is an example of how to read a file using I/O redirection in Bash −

#!/bin/bash

filename="sample.txt"

while IFS= read -r line
do
   echo $line
done < "$filename"

In the above example, we define the file's name as filename. We then use the while loop with I/O redirection to read the file's content line by line. The IFS= read -r line command reads each line of the file and assigns it to the line variable. The IFS is a Bash variable used to set the input field separator, and the -r option prevents backslashes from being interpreted as escape characters. We then use the echo command to print the line to the standard output.

Reading a File Using the cat Command

The cat command is a Unix command used to concatenate and display files. We can use the cat command with a while loop to read a file in Bash.

Here is an example of how to read a file using the cat command in Bash −

#!/bin/bash

filename="sample.txt"

cat "$filename" | while read line
do
   echo $line
done

In the above example, we define the file's name as filename. We then use the cat command to display the contents of the file and then pipe it to the while loop. The while loop reads the output of the cat command line by line and prints it using the echo command.

Reading a File Using the IFS Variable

Another way to read a file in Bash is by using the IFS (Internal Field Separator) variable. The IFS variable is used to specify the delimiter that separates fields in a line of text. By default, the IFS variable is set to space, tab, and newline.

Here is an example of how to read a file using the IFS variable in Bash −

#!/bin/bash
filename="sample.txt"
while IFS= read -r line
do
echo $line
done < "$filename"

In the above example, we define the filename as before. The while loop reads the file line by line, and we set the IFS variable to null to read the entire line instead of just the first word. The -r option is used to prevent backslashes from being interpreted as escape characters. Finally, the '<' character is used to redirect the input from the file.

Reading a File Using the read Command

Another way to read a file in Bash is by using the read command. The read command is used to read a line of input from the standard input or a file descriptor. We can use the read command with a while loop to read a file in Bash.

Here is an example of how to read a file using the read command in Bash −

#!/bin/bash
filename="sample.txt"
while read line
do
echo $line
done < "$filename"

In the above example, we define the filename as before. The while loop reads the file line by line using the read command, and the '<' character is used to redirect the input from the file.

Reading a File Using the awk Command

The awk command is a powerful tool for text processing and data extraction in Unix-like operating systems. We can use the awk command with a while loop to read a file in Bash.

Here is an example of how to read a file using the awk command in Bash −

#!/bin/bash
filename="sample.txt"
awk '{print}' "$filename" | while read line
do
echo $line
done

In the above example, we define the filename as before. The awk command reads the file and prints each line to standard output. The output of the awk command is then piped to the while loop, which reads the output line by line and prints it using the echo command.

Conclusion

In this article, we have discussed different ways to read a file in Bash using a while loop. We have covered the cat command, the IFS variable, the read command, and the awk command. Each of these methods has its advantages and disadvantages, and the choice of method depends on the specific requirements of the task at hand.

Reading a file in Bash is a fundamental task, and it is essential to know how to do it efficiently and effectively. With the techniques discussed in this article, you can read and process files in Bash with ease.

Updated on: 26-Jun-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements