Reading a file line by line helps you process each line separately. This is useful when you want to handle big files or extract information step by step.
Reading files line by line (while read) in Bash Scripting
while IFS= read -r line; do # commands using "$line" done < filename
IFS= prevents trimming leading/trailing spaces.
-r stops backslash escapes from being interpreted.
while read line; do echo "$line" done < file.txt
while IFS= read -r line; do echo "$line" done < file.txt
while IFS= read -r line; do echo "Line: $line" done < file.txt
This script reads a file named sample.txt line by line. It prints each line with its number. It also checks if the file exists before reading.
#!/bin/bash # Read file line by line and print each line with line number filename="sample.txt" if [[ ! -f "$filename" ]]; then echo "File not found: $filename" exit 1 fi count=1 while IFS= read -r line; do echo "Line $count: $line" ((count++)) done < "$filename"
Always quote variables like "$line" to preserve spaces and special characters.
Use IFS= and -r to avoid common pitfalls with spaces and backslashes.
Redirect the file into the loop using done < filename to read from the file.
Use while IFS= read -r line to read files line by line safely.
Process each line inside the loop for automation or data extraction.
Always check if the file exists before reading to avoid errors.
Practice
while IFS= read -r line; do echo "$line"; done < file.txt do in a bash script?Solution
Step 1: Understand the
The loop reads input line by line from the redirected filewhile readloopfile.txt.Step 2: Check the action inside the loop
Each line read is stored in variablelineand printed withecho.Final Answer:
Reads the filefile.txtline by line and prints each line. -> Option DQuick Check:
Line-by-line reading = A [OK]
while read loops read files line by line [OK]- Thinking it reads the whole file at once
- Confusing input redirection with output
- Ignoring the role of
IFS=and-r
Solution
Step 1: Identify input redirection for reading
The correct way to read from a file is using input redirection:< file.txt.Step 2: Check the loop syntax
while read line; do ... doneis the standard loop to read lines.Final Answer:
while read line; do echo $line; done < file.txt -> Option AQuick Check:
Input redirection with < = C [OK]
< file.txt to feed file lines into the loop [OK]- Using output redirection (>) instead of input (<)
- Piping file name instead of redirecting input
- Missing the
-roption to prevent backslash escapes
file.txt contains:apple banana cherry
Script:
while IFS= read -r line; do echo "Fruit: $line"; done < file.txt
Solution
Step 1: Understand the loop reading each line
The loop reads each line fromfile.txtseparately.Step 2: Check the echo command
Each line is printed prefixed with "Fruit: ".Final Answer:
Fruit: apple Fruit: banana Fruit: cherry -> Option AQuick Check:
Line prefixing with echo = B [OK]
- Expecting all lines printed on one line
- Ignoring the prefix in echo
- Confusing input and output redirection
while read line; do echo $line; done file.txt
Solution
Step 1: Check how the file is provided to the loop
The file must be redirected as input using< file.txt, not as an argument.Step 2: Confirm syntax correctness
The rest of the syntax is correct; variable and echo usage are fine.Final Answer:
Missing input redirection operator < before file.txt -> Option CQuick Check:
Input redirection needed = D [OK]
< file.txt to feed file lines, not argument [OK]- Passing filename as argument instead of redirecting input
- Forgetting semicolon after read (optional here)
- Misusing echo syntax
Solution
Step 1: Understand skipping empty lines
We need to check if the line is not empty using[[ -n $line ]].Step 2: Confirm reading and condition
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt reads lines safely and prints only non-empty lines inside the loop.Final Answer:
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt -> Option BQuick Check:
Condition inside loop filters empty lines = A [OK]
if [[ -n $line ]] inside loop to skip empty lines [OK]- Using external grep after loop (may lose variable scope)
- Not checking for empty lines inside the loop
- Using unnecessary cat command
Ready to prove it?
Pass the test and this pattern moves unknown
