← Back to Bash Course | Chapter 4: Loops | Lesson 14 of 14

Command Output पर Looping

Loops किसी और command के output को line by line process कर सकते हैं, जो logs, file listings, या किसी भी generated text handle करने के लिए shell scripting में सबसे आम real-world patterns में से एक है।
Syntax
bash
while IFS= read -r line; do
  commands using "$line"
done <<< "$data"

command | while IFS= read -r line; do
  commands
done

The while read Idiom

while IFS= read -r line; do ... done <<< "$data" text को line by line process करने का standard safe तरीका है: -r backslash interpretation disable करता है, और IFS= हर line से leading/trailing whitespace strip होने से रोकता है।

इसे <<< (एक here-string) के through feed करना loop को current shell में चलाता है, एक subshell में नहीं।

उदाहरण: The while read Idiom

bash
#!/bin/bash
data=$'line one\nline two\nline three'
while IFS= read -r line; do
    echo "Processing: $line"
done <<< "$data"

Reading a File Line by Line

वही while read idiom input redirection (< file) उपयोग करके एक file के against सीधे काम करता है, here-string के बजाय, बिना कभी पूरी file को पहले एक variable में load किए।

यह बड़ी files के लिए भी अच्छी तरह scale करता है क्योंकि हर line एक समय में एक पढ़ी जाती है।

उदाहरण: Reading a File Line by Line

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

Looping Over Command Output with Process Substitution

while read को एक command के output से process substitution, < <(command), के through feed करना उस subshell problem से बचता है जो piping (command | while read) करती है, क्योंकि loop अब भी current shell में चलता है।

इसका मतलब है loop के अंदर set किए variables इसके खत्म होने के बाद भी visible रहते हैं।

उदाहरण: Looping Over Command Output with Process Substitution

bash
#!/bin/bash
total=0
while IFS= read -r num; do
    total=$((total + num))
done < <(printf "1\n2\n3\n")
echo "Total after loop: $total"

for word in $(command) for Simple Word Lists

जब एक command का output simple, whitespace-separated single-word tokens होना जाना जाता हो बिना embedded spaces के, for word in $(command); do ... done उनके ऊपर iterate करने का एक quick तरीका है।

किसी भी line-structured या spaces वाली चीज़ के लिए, while read safer और ज़्यादा correct choice है।

Warning: यह pattern multi-word lines पर टूट जाता है -- जब एक line का meaning उसे whole रखने पर depend करे तो while read के लिए reach करें।

उदाहरण: for word in $(command) for Simple Word Lists

bash
#!/bin/bash
for status_code in $(printf "200 404 500"); do
    echo "HTTP status: $status_code"
done
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. Multi-word lines के ऊपर loop करने के लिए for line in $(command) उपयोग करना; यह हर space और newline पर word-split करता है, एक से ज़्यादा word वाली किसी भी line को mangle करते हुए -- line-based processing के लिए while read correct tool है।
  2. एक while read loop में pipe करना और फिर confused होना कि loop के अंदर set किए variables बाद में disappear हो जाते हैं; एक pipe के receiving end वाला loop एक subshell में चलता है, तो इसके variable changes parent shell में persist नहीं करते (process substitution इससे बचता है, लेकिन यह एक ज़्यादा advanced fix है)।
  3. यह भूल जाना कि read by default leading/trailing whitespace strip करता है और backslashes interpret करता है जब तक -r (raw mode) उपयोग न हो और IFS adjust न हो -- -r छोड़ देना silently backslashes वाली lines corrupt कर सकता है।
चैप्टर सारांश
  • while IFS= read -r line; do ... done <<< "$data" (या < file) text को line by line process करने का standard, safe idiom है।
  • -r read को backslash escapes interpret करने से रोकता है; IFS= इसे हर line से leading/trailing whitespace trim करने से रोकता है।
  • एक while read loop को एक pipe के through feed करना इसे एक subshell में डालता है, तो यह set किए variables loop के बाद खो जाते हैं; input के लिए इसके बजाय <<< या < file उपयोग करना इसे current shell में रखता है।
  • for word in $(command) को सिर्फ simple, known-safe, single-word-per-item output के लिए reserved रखना चाहिए -- किसी भी line-oriented चीज़ के लिए, while read prefer करें।

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.