Reading Files
In this page:
Reading Files
You can read an entire file with cat, or process it line by line with while read -r line; do ... done < file, which is safer for large files since it doesn't load everything into memory at once. read -r prevents backslashes from being interpreted as escape characters. Line-by-line reading is the standard approach when each line needs individual processing.
Note: Always use read -r (raw mode) unless you specifically want backslash escapes interpreted.
Example: Reading Files
#!/bin/bash
printf "line one\nline two\nline three\n" > data.txt
while read -r line; do
echo "Read: $line"
done < data.txt
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: