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

Array की Basics

एक array एक egg carton जैसा है: एक container जिसमें numbered slots हैं, हर एक अपना item रखता है। आप बताकर कि किस slot में आप चाहते हैं, zero से शुरू करके एक item चुनते हैं।

In this page:

  1. Array Basics
Syntax
bash
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

bash
#!/bin/bash
fruits=("apple" "banana" "cherry")

echo "First fruit: ${fruits[0]}"
echo "Second fruit: ${fruits[1]}"
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. arr = (a b c) spaces के साथ लिखना, जो एक syntax error है।
  2. $arr उपयोग करना और सभी items की उम्मीद करना, जब यह सिर्फ पहला देता है; "${arr[@]}" उपयोग करें।
  3. यह मान लेना कि arrays plain sh में मौजूद हैं, जब वे एक Bash feature हैं।

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.