Loop over contents of file and read line by line in bash – Code Example

In this code example we will share a bash function to loop over contents of input file. It contains a while loop which reads the content line by line.

Code Example –

#!/bin/bash

while read -r line; do
    printf 'Line - %s\n' "$line"
done < "file"

Suppose content of file is –

This is first line
this is second line
this is third line

So, output –

Line - This is first line
Line - This is second line
Line - This is third line

Inspired from Dylan Araps