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

Array की Length

Array length यह count करने जैसा है कि carton में असल में कितने eggs हैं। यह बताता है कि आपकी list में अभी कितने items हैं।

In this page:

  1. Array Length
Syntax
bash
${#array_name[@]}

Array Length

${#array[@]} array में currently मौजूद elements की संख्या देता है। यह दोनों indexed और associative arrays के लिए काम करता है। यह आमतौर पर loop bound के रूप में उपयोग होता है जब numeric index से iterate करना हो बजाय foreach-style iteration के।

Note:
  • ${#array[@]} elements count करता है
  • ${#array[0]} (बिना @) इसके बजाय सिर्फ पहले element के string की length देता है।

उदाहरण: Array Length

bash
#!/bin/bash
items=("pen" "notebook" "eraser" "ruler")
echo "Number of items: ${#items[@]}"
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} उपयोग करना और पहले item की length मिलना, जब count ${#arr[@]} है।
  2. यह उम्मीद करना कि length highest index हो, जब यह items की संख्या है।
  3. यह भूल जाना कि unset indexes gaps छोड़ते हैं, इसलिए count last index plus one से छोटा होता है।

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.