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

Total
0
Shares

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

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like