Indexed Arrays का उपयोग
एक indexed array numbers वाली lockers की एक row जैसा है। आप locker 0 से 4 कभी उपयोग किए बिना locker 5 उपयोग कर सकते हैं, और empty वाले बस empty रहती हैं।
In this page:
Syntax
array_name=(value1 value2 value3)
array_name[index]=value
echo "${array_name[index]}"
Indexed Arrays
एक indexed array 0 से शुरू होने वाले numeric indices उपयोग करता है, और indices का contiguous होना ज़रूरी नहीं है — आप indices 0–4 कभी set किए बिना arr[5]=value assign कर सकते हैं।
${arr[@]} सभी elements में expand होता है, और ${!arr[@]} currently use में सभी indices में expand होता है। यह flexibility दूसरी languages में fixed-size arrays की तुलना में Bash के indexed arrays के लिए unique है।
Note:
${!arr[@]} actually use में indices list करता है, जो मायने रखता है जब एक array में gaps हों।उदाहरण: Indexed Arrays
#!/bin/bash
colors=("red" "green" "blue")
colors[5]="purple"
for i in "${!colors[@]}"; do
echo "Index $i: ${colors[$i]}"
done
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Indexes को 1 से शुरू करना, जब Bash arrays 0 से शुरू होते हैं।
$arr[1]लिखना बजाय${arr[1]}के, जो पहला element[1]के साथ followed print करता है।"${arr[@]}"के आस-पास quotes छोड़ना, जो spaces वाले items को split करता है।
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: