Linux Shell Scripting Cheat Sheet/Read file line by line

Process lines safely using while-read.

Section: Conditionals Loops and Functions

Read file line by line

bash
bash
while IFS= read -r line; do echo "$line"; done < input.txt
Explanation

Preferred pattern for preserving whitespace and backslashes.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Conditionals Loops and Functions
Check whether file exists
Use a shell test in an if statement.
OpenIn sheetbashsame section
Check whether directory exists
Test for directory presence.
OpenIn sheetbashsame section
Loop over items
Iterate through words or filenames.
OpenIn sheetbashsame section
Branch with case
Match input against multiple patterns.
OpenIn sheetbashsame section
Define shell function
Create a reusable shell function.
OpenIn sheetbashsame section
Enable strict shell behavior
Fail script on errors and unset vars.
OpenIn sheetbashsame section