← Back to Bash Course | Chapter 7: String Manipulation | Lesson 1 of 10

Length/Substring के लिए Parameter Expansion

Bash आपको बता सकता है कि एक string में कितने characters हैं और इसका कोई भी छोटा टुकड़ा निकाल सकता है, सब कुछ बिना किसी external program को call किए।
Syntax
bash
${#string}
${string:position:length}
${string:position}

${#s} से String Length Pana

${#s} variable s में currently stored characters की संख्या में expand होता है, जो Bash में standard length-checking idiom है। यह characters count करता है, bytes नहीं, ज़्यादातर standard configurations के लिए locale-aware तरीके से।

उदाहरण: Getting String Length with ${#s}

bash
#!/bin/bash
word="Bash"
echo "The word '$word' has ${#word} characters"

${s:pos:len} से एक Substring निकालना

${s:pos:len} s से zero-based index pos से शुरू करके len characters निकालता है। यह एक purely Bash built-in operation है, cut या awk जैसे किसी external tool की ज़रूरत के बिना।

उदाहरण: Extracting a Substring with ${s:pos:len}

bash
#!/bin/bash
sentence="The quick brown fox"
word=${sentence:4:5}
echo "Extracted word: $word"

Length Omit करना

Length argument छोड़ देना, ${s:pos}, index pos से string के end तक सब कुछ निकालता है। यह convenient है जब आपको ठीक से पता नहीं या care नहीं कि बाकी बचा text कितना लंबा है।

उदाहरण: Omitting the Length

bash
#!/bin/bash
path="/usr/local/bin/mytool"
remainder=${path:5}
echo "Everything after index 5: $remainder"

Negative Starting Position

एक negative pos, एक leading space के साथ लिखा हुआ जैसे ${s: -4}, string के beginning से नहीं बल्कि end से backward count करता है। किसी string के last few characters, जैसे एक file extension, पकड़ने का यह एक quick तरीका है।

Warning: ${filename:-4} (space के बिना) इसके बजाय default-value expansion trigger करेगा, substring extraction नहीं।

उदाहरण: Negative Starting Position

bash
#!/bin/bash
filename="report_final.txt"
last_four=${filename: -4}
echo "Last four characters: $last_four"
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. ${#s} (string s की length) को ${#arr[@]} (एक array की length) से confuse करना; # prefix दोनों cases में 'length of' का मतलब है, लेकिन इसके बाद क्या आता है यह decide करता है कि यह एक string है या array count।
  2. यह मान लेना कि ${s:pos:len} कुछ और tools जैसे one-based indexing उपयोग करता है; Bash string indexing zero-based है, तो ${s:0:1} पहला character है।
  3. यह भूल जाना कि एक negative starting position को minus sign से पहले एक space चाहिए (${s: -3}) ताकि इसे ${var:-default} expansion के रूप में parse होने से बचाया जाए substring extraction के बजाय।
चैप्टर सारांश
  • ${#s} string s में characters की संख्या देता है।
  • ${s:pos:len} zero-based position pos से शुरू करके len characters निकालता है।
  • ${s:pos} में len omit करना pos से string के end तक सब कुछ निकालता है।
  • एक negative pos string के end से backward count करता है, लेकिन :- के साथ ambiguity से बचने के लिए एक leading space या parentheses चाहिए।

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.