← Back to Bash Course | Chapter 5: Functions | Lesson 1 of 11

String की Length

String length एक name tag पर letters count करने जैसा है। Bash एक छोटी trick से आपको बता सकता है कि आपके text में कितने characters हैं।

In this page:

  1. String Length
Syntax
bash
${#variable_name}

String Length

${#variable} एक string variable में characters की संख्या देता है। यह wc जैसी external command call किए बिना string length check करने का एक fast, built-in तरीका है। यह syntax Bash की parameter expansion feature का हिस्सा है।

Note: wc -c से pipe करने के बजाय ${#var} उपयोग करें — यह एक subprocess शुरू करने से बचता है।

उदाहरण: String Length

bash
#!/bin/bash
name="Hello, Bash!"
echo "Length: ${#name}"
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 पर ${#var} उपयोग करना और items की संख्या के बजाय पहले element की length मिलना।
  2. # भूल जाना, जैसे ${var}, जो सिर्फ value देता है।
  3. यह उम्मीद करना कि ${#var} bytes count करेगा, जब यह एक UTF-8 locale में characters count करता है।

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.