← Back to Bash Course | Chapter 2: Variables & Types | Lesson 12 of 13

Arrays का परिचय

एक array एक single variable है जो सिर्फ एक के बजाय values की एक पूरी list रख सकता है, हर एक एक numeric position से accessible।
Syntax
bash
array_name=(value1 value2 value3)
echo "${array_name[index]}"
echo "${array_name[@]}"
echo "${#array_name[@]}"

एक Indexed Array बनाना

आप parentheses के अंदर spaces से separated values list करके एक array literal बनाते हैं, जैसे fruits=(apple banana cherry)। Bash अपने आप 0 से शुरू होने वाले indices assign करता है, इसलिए apple index 0 पर है, banana index 1 पर, और इसी तरह आगे।

उदाहरण: Creating an Indexed Array

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"

सभी Elements Access करना

${arr[@]} array के हर element में separate words के रूप में expand होता है, जो आपको तब चाहिए जब आप contents पर loop कर रहे हों। सिर्फ $arr या ${arr} बिना किसी index के उपयोग करना सिर्फ element 0 को refer करता है, एक common beginner mistake।

उदाहरण: Accessing All Elements

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "All fruits: ${fruits[@]}"
echo "Just index 0 (via \$fruits): $fruits"

Array की Length

${#arr[@]} array में currently store elements की total संख्या देता है, जो loop bounds या validation के लिए useful है। यह ${#arr} या ${#arr[0]} से अलग है, जो first element की string की character length देते हैं।

उदाहरण: Array Length

bash
#!/bin/bash
fruits=(apple banana cherry)
echo "Number of fruits: ${#fruits[@]}"
echo "Length of first element string: ${#fruits[0]}"

एक Array में Append करना

नए elements एक array के end में += operator को एक parenthesized value list के साथ उपयोग करके जोड़े जा सकते हैं। यह array को इसकी current length जाने या इसे scratch से rebuild किए बिना बड़ा करता है।

Warning: Arrays में एक पूरी deep dive -- associative arrays, slicing, और बहुत कुछ -- बाद में अपने खुद के chapter में आती है।

उदाहरण: Appending to an Array

bash
#!/bin/bash
fruits=(apple banana)
fruits+=(cherry)
fruits+=(date fig)
echo "Fruits now: ${fruits[@]}"
echo "Count: ${#fruits[@]}"
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 literal बनाते समय parentheses भूल जाना; fruits=apple banana एक array नहीं बनाता, यह banana को fruits=apple को environment prefix के रूप में उपयोग करते हुए एक command के रूप में चलाने की कोशिश करता है।
  2. $fruits (बिना index) उपयोग करना और सभी elements की उम्मीद करना; वह सिर्फ आपको पहला element (index 0) देता है -- पूरे array के लिए आपको ${fruits[@]} चाहिए।
  3. यह मान लेना कि array indices हमेशा 0 से शुरू होने वाले contiguous हैं बिना किसी gap के; Bash indexed arrays असल में sparse हैं, इसलिए एक element हटाना बाद के elements को नीचे shift करने के बजाय एक gap छोड़ देता है।
चैप्टर सारांश
  • arr=(one two three) से एक indexed array बनाएं; indices default रूप से 0 से शुरू होते हैं।
  • ${arr[0]} से एक single element access करें; ${arr[@]} से सभी elements access करें।
  • ${#arr[@]} array में currently मौजूद elements की संख्या देता है।
  • Bash arrays sparse हैं -- unset elements remove और renumber होने के बजाय index sequence में gaps छोड़ते हैं।

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.