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

Indexed Arrays विस्तार से

Indexed arrays एक single variable name के under values की एक ordered list store करते हैं, हर एक 0 से शुरू होने वाली एक numeric position से accessible।
Syntax
bash
declare -a array_name
array_name=(value1 value2 value3)
array_name[index]=value

Indexed Arrays Declare और Assign करना

Indexed arrays को explicitly declare -a arrname से declare किया जा सकता है या values की एक parenthesized list assign करके बस बनाया जा सकता है, arr=(a b c)। दोनों तरीकों में, Bash elements को index 0 से शुरू करके अपने आप number करता है।

उदाहरण: Declaring and Assigning Indexed Arrays

bash
#!/bin/bash
declare -a numbers
numbers=(10 20 30)
echo "First: ${numbers[0]}, Second: ${numbers[1]}, Third: ${numbers[2]}"

Specific Indices को Assign करना

Individual elements को directly index से set किया जा सकता है, arr[5]=value, इससे पहले उसके indices assign किए होने की ज़रूरत के बिना। यह एक sparse array produce कर सकता है, जहां lowest और highest के बीच कुछ numeric indices बस exist नहीं करते।

Warning: Array में केवल 2 असली elements हैं भले ही highest index 5 है, क्योंकि indices 1-4 कभी assign नहीं हुए।

उदाहरण: Assigning to Specific Indices

bash
#!/bin/bash
declare -a grid
grid[0]="start"
grid[5]="end"
echo "grid[0] = ${grid[0]}"
echo "grid[5] = ${grid[5]}"
echo "Number of actual elements: ${#grid[@]}"

एक Existing Element को Modify करना

एक existing array element को बिल्कुल एक normal variable जैसे overwrite किया जा सकता है, इसके specific index को एक नया value assign करके। यह array में किसी भी और element को affect नहीं करता।

उदाहरण: Modifying an Existing Element

bash
#!/bin/bash
colors=(red green blue)
colors[1]="yellow"
echo "Updated array: ${colors[@]}"

सभी Elements और इनका Count Print करना

${arr[@]} सभी elements को separate words के रूप में expand करता है, और ${#arr[@]} currently present elements की total count देता है। ये दो expansions Bash में लगभग हर array-processing pattern की backbone हैं।

उदाहरण: Printing All Elements and Their Count

bash
#!/bin/bash
planets=(Mercury Venus Earth Mars)
echo "All planets: ${planets[@]}"
echo "Total count: ${#planets[@]}"
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. यह मान लेना कि array indices को 0 से शुरू करके consecutively assign किया जाना चाहिए; आप directly किसी भी index को assign कर सकते हैं, जैसे arr[10]=x, जो बीच में gaps वाला एक sparse array बनाता है।
  2. यह भूल जाना कि ${arr[@]} बनाम ${arr[*]} quoted होने पर अलग होते हैं, वही subtlety जो positional parameters के लिए $@ बनाम $* में है।
  3. एक array की length ${#arr} से लेने की कोशिश करना बजाय ${#arr[@]} के; पहला सिर्फ पहले element का character length देता है।
चैप्टर सारांश
  • Explicitly declare -a arr से declare करें या बस directly assign करें, arr=(a b c)।
  • Individual elements को explicit index से assign या read किया जा सकता है: arr[2]=value, ${arr[2]}।
  • Arrays sparse हो सकते हैं -- arr[100] को assign करने से इससे पहले 100 empty slots नहीं बनते।
  • ${arr[@]:offset:length} और इस chapter में बाद में cover किए गए दूसरे expansions सब यहां के indexed array fundamentals पर बनते हैं।

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.