How To

How To Read A File Line By Line In Bash

Introduction

Reading a file line by line is a common task in Bash scripting. Many Linux scripts need to process text files, configuration files, log files, wordlists, usernames, IP addresses, or command output one line at a time.

If you are learning Bash scripting, this is an important topic because file reading is used in automation, log analysis, cybersecurity tasks, system monitoring, and data processing. For example, you can read a list of IP addresses and ping each one, read usernames from a file, or scan log files for suspicious activity.

Create A Sample File

First, create a sample text file:

nano users.txt

Add the following lines:

adminkalirootguestdeveloper

Save the file and exit the editor.

Basic Method To Read A File Line By Line

The most common way to read a file line by line in Bash is by using a while read loop.

Create a script:

nano read-file.sh

Add the following code:

#!/bin/bashwhile read linedo    echo "User: $line"done < users.txt

Give execute permission and run it:

chmod +x read-file.sh./read-file.sh

Output:

User: adminUser: kaliUser: rootUser: guestUser: developer

In this script, Bash reads each line from users.txt and stores it in the variable line.

Better Method Using read -r

The recommended method is to use read -r. This prevents backslashes from being treated as escape characters.

#!/bin/bashwhile read -r linedo    echo "Line: $line"done < users.txt

For most scripts, read -r is safer and better.

Read A File And Skip Empty Lines

Sometimes files may contain blank lines. You can skip them using an if condition.

#!/bin/bashwhile read -r linedo    if [[ -z "$line" ]]; then        continue    fi    echo "Processing: $line"done < users.txt

The -z operator checks whether the line is empty.

Cybersecurity Example: Read IP Addresses From A File

Create an IP list:

nano ips.txt

Add:

192.168.1.1192.168.1.2192.168.1.3

Now create a script:

#!/bin/bashwhile read -r ipdo    echo "Pinging $ip"    ping -c 1 "$ip"done < ips.txt

This script reads each IP address and runs a ping command.

Conclusion

Reading a file line by line in Bash is a basic but powerful scripting skill. The best method is using a while read -r line loop with input redirection.

This technique is useful for processing logs, reading user lists, scanning IP addresses, handling configuration files, and building cybersecurity automation scripts. Once you understand this concept, you can create more practical and powerful Bash scripts in Linux.

Cyber Defence

Recent Posts

Install Nginx on Ubuntu 16.04: UFW, PPA, and Config Structure

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

9 hours ago

Install Nginx on Ubuntu 18.04: UFW, Service Control, and Config

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

9 hours ago

Secure Nginx with Let’s Encrypt on Ubuntu 18.04: SSL Setup Guide

Let's Encrypt is a free, automated, and open certificate authority run by the Internet Security Research…

9 hours ago

Install PHP on Ubuntu 18.04: Apache, Nginx FPM, and Ondrej PPA

PHP is the most widely used server-side scripting language for web development. Ubuntu 18.04 ships with PHP…

9 hours ago

Install Skype on Ubuntu 18.04: .deb Package and Auto-Updates

Skype is one of the most widely used communication platforms in the world. It lets you…

9 hours ago

Install Samba on Ubuntu 18.04: Configure Shares and User Access

Samba is a free, open-source implementation of the SMB/CIFS network protocol that lets Linux servers share…

1 day ago