There are several ways to process the contents of a file in bash scripts. You might want to perform actions such as reading and manipulating data line by line or extracting specific information. This tutorial explores different methods to iterate through file content in a Bash script.
1: Using a while loop
One common approach is using a while loop along with the read command. Here’s an example:
1#!/bin/bash
2
3while read -r line && [[ -n "$line" ]]; do
4 # Process line
5 echo "Processing: $line"
6done < "filename.txt"
Explanation:
In this case, while loop reads each line from the file specified after the input redirection (<) symbol. The read -r line command assigns each line to the variable line. Inside the loop, we are simply printing the line content, but we can perform any other operations too.
2: Using a for loop
Another way to iterate through file content is by using a for loop with the cat command.
1#!/bin/bash
2
3for line in $(cat filename.txt); do
4 # Process line
5 echo "Processing: $line"
6done
Explanation:
The cat command reads and outputs the entire contents of the file. The for loop iterates over each item separated by whitespace (default behavior of $(cat filename.txt)).
3: Using IFS and readarray (or mapfile)
If you want to read all lines of a file into an array, you can use IFS (Internal Field Separator) along with readarray or mapfile commands.
1#!/bin/bash
2
3# Read the file into an array
4readarray -t lines < "filename.txt"
5
6for line in "${lines[@]}"; do
7 # Process line
8 echo "Processing: $line"
9done
Here, The readarray -t lines < "filename.txt" reads the lines into an array named lines. The for loop iterates over each element of the array.
If we have different delimiter than new line, we can use IFS to modify to that specific value.


Comments