← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 6 of 13

File को Line by Line पढ़ना

आप एक script को एक text file में एक बार में एक line जाने दे सकते हैं, हर line पर कुछ logic चलाते हुए।
Syntax
bash
while IFS= read -r line; do
  commands using "$line"
done < file_name

Standard while-read Idiom

done के बाद < file के साथ एक file को while read loop में redirect करना किसी subshell की ज़रूरत के बिना end-of-file तक हर iteration में एक line पढ़ता है। IFS= read -r whitespace को preserve करता है और backslash escaping को disable करता है।

उदाहरण: The Standard while-read Idiom

bash
#!/bin/bash
printf "apple\nbanana\ncherry\n" > fruits.txt
while IFS= read -r line; do
    echo "Fruit: $line"
done < fruits.txt
rm -f fruits.txt

Piping से Variables कैसे खोते हैं

एक command को while read में pipe करना, जैसे cat file | while read line; do ...; done, loop को एक subshell में चलाता है क्योंकि यह pipe के right side पर है। उस loop के अंदर changed कोई भी variables loop खत्म होने पर गायब हो जाते हैं।

Warning: अगर आपको एक file पढ़ते समय state accumulate करनी हो, तो while loop में pipe नहीं बल्कि < file redirection उपयोग करें।

उदाहरण: Why Piping Loses Variables

bash
#!/bin/bash
printf "1\n2\n3\n" > numbers.txt
count=0
cat numbers.txt | while read -r n; do
    count=$((count + 1))
done
echo "count after piped loop (lost): $count"

count=0
while read -r n; do
    count=$((count + 1))
done < numbers.txt
echo "count after redirected loop (kept): $count"
rm -f numbers.txt

Trailing Newline के बिना Last Line पढ़ना

अगर एक file की last line में कोई trailing newline न हो, plain read अब भी वह final line का text return करने में succeed होता है लेकिन एक non-zero status return करता है, जो loop body को skip होने का कारण बन सकता है। || [[ -n $line ]] जांचना इस edge case को handle करता है।

उदाहरण: Reading the Last Line Without a Trailing Newline

bash
#!/bin/bash
printf "one\ntwo\nthree" > no_trailing_newline.txt
while IFS= read -r line || [[ -n "$line" ]]; do
    echo "Got: $line"
done < no_trailing_newline.txt
rm -f no_trailing_newline.txt
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक while read loop में pipe करना और फिर यह सोचना कि loop के अंदर set किए variables इसके बाद क्यों मौजूद नहीं हैं; एक piped loop एक subshell में चलता है।
  2. read से पहले IFS= भूल जाना, जो हर line से leading/trailing whitespace unexpectedly trim कर देता है।
  3. while read के बजाय for line in $(cat file) उपयोग करना; for version whitespace पर split करता है और spaces वाली lines पर break हो जाता है।
चैप्टर सारांश
  • while IFS= read -r line; do ... done < file एक file को line by line पढ़ने का standard, safe idiom है।
  • File को < file से redirect करना इसे done के बाद current shell में रखता है, इसमें pipe करने के विपरीत।
  • IFS= हर line से leading/trailing whitespace strip होने से रोकता है।
  • read end-of-file पर non-zero return करता है, जो naturally while loop को खत्म कर देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.