Bash Read File line by line – Example

Bash Read File line by line

To Read File line by line in Bash Scripting, following are some of the ways explained in detail.

Example – Using While Loop

Following is the syntax of reading file line by line in Bash using bash while loop :

Syntax

Input File

Bash Script File

 

Example – Preventing backslash escapes

To prevent backslash escapes while reading file line by line in bash, use -r option of read command :

Syntax

Bash Script File

 

Example – Prevent trimming of leading or trailing white-spaces.

To prevent trimming of leading or trailing white-spaces while reading file line by line in bash, use IFS command in conjunction with read command :

Syntax

Bash Script File

 

Conclusion :

In this Bash Tutorial, we have learnt to read a file line by line in bash scripting with examples.