← Back to Bash Course | Chapter 6: Arrays | Lesson 3 of 10

Indexed Arrays का उपयोग

एक indexed array numbers वाली lockers की एक row जैसा है। आप locker 0 से 4 कभी उपयोग किए बिना locker 5 उपयोग कर सकते हैं, और empty वाले बस empty रहती हैं।

In this page:

  1. Indexed Arrays
Syntax
bash
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

bash
#!/bin/bash
colors=("red" "green" "blue")
colors[5]="purple"

for i in "${!colors[@]}"; do
    echo "Index $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. Indexes को 1 से शुरू करना, जब Bash arrays 0 से शुरू होते हैं।
  2. $arr[1] लिखना बजाय ${arr[1]} के, जो पहला element [1] के साथ followed print करता है।
  3. "${arr[@]}" के आस-पास quotes छोड़ना, जो spaces वाले items को split करता है।

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.