Syntax

Bash Read a File Line by Line Syntax

In Bash, read a file line by line: loops over each line of a file — while IFS= read -r line; do echo "$line" done < file.txt

Quick Answer

Syntax
while IFS= read -r line; do echo "$line" done < file.txt
BashSyntax

Read a File Line by Line

Code example
while IFS= read -r line; do echo "$line" done < file.txt

Loops over each line of a file.

Learn More

How to read a file line by line in Bash

  1. Open your Bash file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Bash Read a File Line by Line Syntax

Use while IFS= read -r line; do echo "$line" done < file.txt.

Related Syntax References

Frequently asked questions

What is the Bash syntax for read a file line by line?

Use this pattern: while IFS= read -r line; do echo "$line" done < file.txt

Can I copy this Bash code example?

Yes. Use Copy code and adapt the example to your program.

Is this Bash syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify