Array की Basics
एक array एक egg carton जैसा है: एक container जिसमें numbered slots हैं, हर एक अपना item रखता है। आप बताकर कि किस slot में आप चाहते हैं, zero से शुरू करके एक item चुनते हैं।
In this page:
Syntax
array_name=(value1 value2 value3)
echo "${array_name[0]}"
Array Basics
Bash arrays एक single variable में multiple values store करते हैं, name=(value1 value2 value3) से declared। Individual elements को ${name[index]} से access किया जाता है, zero-based indexing उपयोग करते हुए।
Arrays उन कुछ compound data structures में से एक हैं जो Bash natively प्रदान करता है।
Note:
Array expansions को हमेशा quote करें, जैसे
"${arr[@]}", spaces वाले elements को intact रखने के लिए।उदाहरण: Array Basics
#!/bin/bash
fruits=("apple" "banana" "cherry")
echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"
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. #}
आम गलतियां
arr = (a b c)spaces के साथ लिखना, जो एक syntax error है।$arrउपयोग करना और सभी items की उम्मीद करना, जब यह सिर्फ पहला देता है;"${arr[@]}"उपयोग करें।- यह मान लेना कि arrays plain
shमें मौजूद हैं, जब वे एक Bash feature हैं।
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: