Array Elements पर Looping
In this page:
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
#!/bin/bash
fruits=("apple" "kiwi fruit" "cherry")
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
Login to try C/C++/Java/PHP code in the editor
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[@]}
#!/bin/bash
fruits=("apple" "banana" "cherry")
for idx in "${!fruits[@]}"; do
echo "Index $idx: ${fruits[idx]}"
done
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
colors=(red green blue)
for (( i = 0; i < ${#colors[@]}; i++ )); do
echo "colors[$i] = ${colors[i]}"
done
Login to try C/C++/Java/PHP code in the editor
${arr[@]}को"${arr[@]}"के रूप में quote करना भूल जाना, जो spaces वाले elements के लिए protection खो देता है और उन्हें कई words में split कर देता है।${arr[@]}के मतलब वाली जगह${arr[*]}उपयोग करना एक loop के अंदर;[*]सब कुछ एक single string में जोड़ता है (IFS के subject), तो उसके ऊपर (quoted) एकforloop हर element iterate करने के बजाय सिर्फ एक combined item देखेगा।${!arr[@]}से loop करते समय यह मान लेना कि array indices हमेशा sequential होते हैं; अगर elementsunsetसे हटाए गए हों, तो 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 रहें।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: