How to Read a File Line By Line in Bash

When writing Bash scripts, you will sometimes find yourself in situations where you need to read a file line by line. For example, you may have a text file containing data that should be processed by the script.
This guide explains how to read a file line by line with a while loop, loop over lines with for, read a whole file into a variable, and process the output of a command.
read command’s -p prompt option or other read flags, see the Bash read Command
guide.Quick Reference
For a printable quick reference, see the bash cheatsheet .
| Method | When to use |
|---|---|
while IFS= read -r line; do ... done < file | Standard file reading; preserves all whitespace |
mapfile -t lines < file | Read the whole file into an array, then loop with for (Bash 4.0+) |
content=$(<file) | Read the whole file into one variable |
while IFS=, read -r f1 f2; do ... done < file | Split each line on a delimiter |
while IFS= read -r line; do ... done < <(cmd) | Read from command output, variables stay in scope |
while IFS= read -r -u9 line; do ... done 9< file | Loop body also reads from stdin |
Reading a File Line By Line Syntax
The standard syntax for reading a file line by line is:
while IFS= read -r line; do
printf '%s\n' "$line"
done < input_fileOr the equivalent single-line version:
while IFS= read -r line; do printf '%s\n' "$line"; done < input_fileHow it works:
input_fileis redirected into the while loop as standard input.- The
readcommand reads one line at a time and assigns it to thelinevariable. IFS=clears the Internal Field Separator so that leading and trailing whitespace in each line is preserved. Without it,readstrips spaces and tabs from both ends of the line.-rdisables backslash interpretation so that backslashes in the file are treated as literal characters. Without it,readmay silently alter data containing backslash sequences.printfis used instead ofechoto avoid unexpected behavior when a line starts with a value like-ethatechomight interpret as an option.- Once all lines are processed, the while loop terminates.
Reading a File Line By Line Examples
Let us take a look at the following example. Suppose we have a file named distros.txt containing a list of Linux distributions and their package managers, separated by a comma:
Ubuntu,apt
Debian,apt
AlmaLinux,dnf
Arch Linux,pacman
Fedora,dnfTo read the file line by line and print each line:
while IFS= read -r line; do
printf '%s\n' "$line"
done < distros.txtThe code reads the file line by line, assigns each line to a variable, and prints it. The output is the same as running cat distros.txt.
To print only the distributions that use apt, use an if statement
to check if the line contains the substring
apt:
while IFS= read -r line; do
if [[ "$line" == *"apt"* ]]; then
printf '%s\n' "$line"
fi
done < distros.txtUbuntu,apt
Debian,aptSplitting Lines into Fields
You can pass more than one variable to read to split each line into fields based on IFS. The first field goes into the first variable, the second into the second, and so on. If there are more fields than variables, the last variable gets the rest of the line, separators included.
The following example sets IFS to a comma and splits each line into distro and pm:
while IFS=, read -r distro pm; do
printf '%s is the package manager for %s\n' "$pm" "$distro"
done < distros.txtapt is the package manager for Ubuntu
apt is the package manager for Debian
dnf is the package manager for AlmaLinux
pacman is the package manager for Arch Linux
dnf is the package manager for FedoraHere, read assigns the text before the comma to distro and the remaining text to pm. The printf format string places $pm first, so each output line starts with the package manager.
Alternative File Reading Methods
Reading a File into an Array with mapfile
The mapfile built-in (also available under the name readarray) reads all lines of a file into an array in a single step. It requires Bash 4.0 or newer. The Bash that ships with macOS is version 3.2 and does not have it.
The following command reads distros.txt into an array named lines:
mapfile -t lines < distros.txtThe -t option strips the trailing newline from each line, and each element of the lines array holds one line of the file. You can then iterate over the array with a for loop
:
mapfile -t lines < distros.txt
for line in "${lines[@]}"; do
printf '%s\n' "$line"
doneLooping Over Lines with a for Loop
A common first attempt at reading a file with for is to loop over the output of cat:
for line in $(cat distros.txt); do
printf '%s\n' "$line"
doneUbuntu,apt
Debian,apt
AlmaLinux,dnf
Arch
Linux,pacman
Fedora,dnfThe file has five lines, but the loop ran six times. Bash splits the unquoted command output on every space, tab, and newline, so Arch Linux,pacman became two items. Glob characters like * in the file would also expand to matching filenames in the current directory.
If you prefer a for loop over while read, the mapfile loop from the previous section is the correct way to get one. Each element of the quoted array holds one full line, and no splitting or glob expansion takes place. On Bash 3.2, stick with the while loop.
Reading a File into a Variable
To read the whole file into a single string variable instead of an array, use $(<file):
content=$(<distros.txt)The $(<file) form is a Bash shortcut for $(cat file) that does not start an external process. Command substitution removes the trailing newline, so content ends with Fedora,dnf. Always quote the variable when you use it ("$content"). Without quotes, Bash splits the value on spaces and newlines, and Arch Linux,pacman turns into two words again.
If you need only the first line of the file, read can do it without a loop:
IFS= read -r first_line < distros.txt
printf '%s\n' "$first_line"Ubuntu,aptThe read command stops at the first newline, so first_line holds only the Ubuntu entry.
Reading a Variable Line by Line
A here string (<<<) passes the value of a variable to the loop as standard input. It is the simplest way to read a variable line by line:
while IFS= read -r line; do
printf '%s\n' "$line"
done <<< "$content"The loop prints the five lines stored in content. Bash adds a newline to the end of every here string, so an empty variable still produces one pass through the loop with an empty line. If that matters in your script, check the variable with [[ -n "$content" ]] before the loop.
You can pass a file the same way with <<< "$(cat input_file)", but there is no reason to. Command substitution strips all trailing newlines from the output, and the here string then adds one newline. As a result, trailing blank lines and the file’s original final-newline state are not preserved. Redirecting the file with < avoids both problems.
Using Process Substitution
Process substitution lets you use the output of a command as the input file. This is the recommended way to read from a command rather than a static file, because it keeps the loop in the current shell, unlike piping, which runs the loop body in a subshell and causes variables to be lost after the loop ends:
while IFS= read -r line; do
printf '%s\n' "$line"
done < <(grep "apt" distros.txt)The < <(command) syntax redirects the command output as standard input to the loop without creating a subshell for the loop body.
Using a File Descriptor
You can open the file on a specific file descriptor and pass it to read with the -u option:
while IFS= read -r -u9 line; do
printf '%s\n' "$line"
done 9< input_fileUse a file descriptor number from 3 to 9. The Bash manual warns that descriptors greater than 9 may conflict with the ones the shell uses internally. This approach is useful when the loop body also reads from standard input (for example, prompting the user).
Troubleshooting
Variables set inside the loop are empty after the loop ends
This happens when read runs in a pipe subshell. Replace command | while IFS= read -r line with while IFS= read -r line; do ...; done < <(command). Process substitution keeps the loop in the current shell so variables are accessible after the loop.
Leading or trailing whitespace is being stripped
The IFS= assignment before read is missing or incorrect. Make sure you write IFS= read -r line (with no characters between = and read), not IFS=" " read or just read -r line.
The last line of the file is not processed
If the file does not end with a newline, read returns a non-zero exit code on the final line and the loop exits before processing it. To handle this, add || [[ -n "$line" ]] to the while condition: while IFS= read -r line || [[ -n "$line" ]]; do.
The loop stops early or skips lines
A command inside the loop is reading from standard input. Inside the loop, standard input is the file itself. Programs such as ssh and ffmpeg read standard input by default and swallow the remaining lines. Redirect that command’s input from /dev/null (for ssh, the -n option does the same), or open the file on a separate descriptor as shown in the file descriptor section above.
Lines from a Windows file never match
Files created on Windows end each line with a carriage return and a newline (CRLF). The read command removes only the newline, so every line keeps a hidden \r at the end, and a test such as [[ "$pm" == "apt" ]] never succeeds. Run cat -A file to check: lines that end in ^M$ have Windows line endings. Strip the carriage return from the last field inside the loop with pm=${pm%$'\r'}, or convert the file once with dos2unix.
The loop produces garbled output on binary files
The while read pattern is designed for text files. Binary files contain null bytes and non-printable characters that read cannot handle reliably. Use dedicated tools for binary data.
Conclusion
The while IFS= read -r line; do ... done < file pattern is the reliable, portable way to read a file line by line in Bash. For details on the read built-in and its options, see the Bash read Command
guide.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page