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

Array Elements पर Looping

आप एक array में store की हर value को एक समय में एक for loop उपयोग करके process कर सकते हैं, और optionally हर value के साथ index भी पा सकते हैं।
Syntax
bash
for element in "${array_name[@]}"; do
  commands using "$element"
done

for index in "${!array_name[@]}"; do
  commands using "${array_name[index]}"
done

Iterating Values Directly

for val in "${arr[@]}"; do ... done आपको बारी-बारी हर array element देता है, पूरी तरह preserved भले ही एक element में spaces हों, जब तक आप expansion को quote करें। यह array में हर item process करने का सबसे आम और straightforward तरीका है।

उदाहरण: Iterating Values Directly

bash
#!/bin/bash
fruits=("apple" "kiwi fruit" "cherry")
for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done

Iterating Indices with ${!arr[@]}

"${!arr[@]}" उन indices की list में expand होता है जो currently array में मौजूद हैं (values नहीं), जो उपयोगी है जब आपको हर element की position जानना हो, खासकर क्योंकि Bash arrays में gaps हो सकते हैं।

आप फिर loop के अंदर ${arr[idx]} से हर value look up करते हैं।

उदाहरण: Iterating Indices with ${!arr[@]}

bash
#!/bin/bash
fruits=("apple" "banana" "cherry")
for idx in "${!fruits[@]}"; do
    echo "Index $idx: ${fruits[idx]}"
done

Why Quoting the Expansion Matters

${arr[@]} को unquoted छोड़ना word splitting को spaces वाले किसी element को कई अलग loop iterations में तोड़ने देता है, silently आपका data corrupt करते हुए।

इसे "${arr[@]}" के रूप में quote करना गारंटी देता है कि हर element exactly एक item के रूप में treat हो, इसकी contents चाहे जो भी हों।

उदाहरण: Why Quoting the Expansion Matters

bash
#!/bin/bash
items=("first item" "second item")
echo "Unquoted (broken):"
for i in ${items[@]}; do
    echo "  [$i]"
done
echo "Quoted (correct):"
for i in "${items[@]}"; do
    echo "  [$i]"
done

C-Style Loop Over Array Length

एक numeric C-style for loop भी index से एक array walk कर सकता है, 0 से लेकर ${#arr[@]} तक (इसे शामिल किए बिना), जो array का element count है, count करते हुए।

यह approach handy है जब आपको specifically neighboring indices compare या modify करना हो, सिर्फ order में values पढ़ना नहीं।

उदाहरण: C-Style Loop Over Array Length

bash
#!/bin/bash
colors=(red green blue)
for (( i = 0; i < ${#colors[@]}; i++ )); do
    echo "colors[$i] = ${colors[i]}"
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. ${arr[@]} को "${arr[@]}" के रूप में quote करना भूल जाना, जो spaces वाले elements के लिए protection खो देता है और उन्हें कई words में split कर देता है।
  2. ${arr[@]} के मतलब वाली जगह ${arr[*]} उपयोग करना एक loop के अंदर; [*] सब कुछ एक single string में जोड़ता है (IFS के subject), तो उसके ऊपर (quoted) एक for loop हर element iterate करने के बजाय सिर्फ एक combined item देखेगा।
  3. ${!arr[@]} से loop करते समय यह मान लेना कि array indices हमेशा sequential होते हैं; अगर elements unset से हटाए गए हों, तो gaps मौजूद हो सकते हैं, इसलिए index-based loops को यह नहीं मानना चाहिए कि 0 से length-1 तक हर number मौजूद है।
चैप्टर सारांश
  • for val in "${arr[@]}" हर array element को एक अलग, पूरी तरह preserved value के रूप में iterate करता है।
  • for idx in "${!arr[@]}" array की indices (! prefix indices request करता है, values नहीं) के ऊपर iterate करता है।
  • जब आपको index और value दोनों साथ चाहिए तो दोनों combine करें: "${!arr[@]}" के ऊपर loop करें और ${arr[idx]} look up करें।
  • Loops में हमेशा "${arr[@]}"/"${!arr[@]}" quote करें ताकि spaces वाले elements intact रहें।

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.