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

Substrings/Prefixes जाँचना

Bash यह जांच सकता है कि एक string में कोई particular piece of text है, से शुरू होता है, या पर खत्म होता है, case या glob wildcards के साथ [[ ]] test उपयोग करके।
Syntax
bash
[[ $string == *substring* ]]
[[ $string == prefix* ]]
[[ $string == *suffix ]]

Checking if a String Contains a Substring

[[ $str == *substring* ]] यह जांचने के लिए glob wildcards उपयोग करता है कि $str के अंदर कहीं भी substring आता है या नहीं, क्योंकि हर side पर unquoted * किसी भी surrounding text (कुछ भी न होना शामिल) से match करता है।

यह grep की ज़रूरत के बिना एक simple contains check के लिए standard idiom है।

उदाहरण: Checking if a String Contains a Substring

bash
#!/bin/bash
sentence="The quick brown fox jumps"
if [[ $sentence == *brown* ]]; then
    echo "Found 'brown' in the sentence"
fi

Checking a Prefix or Suffix

[[ $str == prefix* ]] जांचता है कि $str prefix से शुरू होता है, और [[ $str == *suffix ]] जांचता है कि यह suffix पर खत्म होता है, wildcard को सिर्फ उस side पर रखकर जिसकी आपको परवाह नहीं।

यह naturally पढ़ता है और शुरुआत या आखिर compare करने के लिए manual substring slicing से बचाता है।

उदाहरण: Checking a Prefix or Suffix

bash
#!/bin/bash
filename="report_2024.csv"
if [[ $filename == report_* ]]; then
    echo "Starts with 'report_'"
fi
if [[ $filename == *.csv ]]; then
    echo "Ends with '.csv'"
fi

Using case for Substring Checks

case [[ ]] जैसी ही glob-based substring, prefix, और suffix checks perform कर सकता है, जो खासकर readable है जब आप एक साथ कई अलग possible patterns के बीच distinguish कर रहे हों।

हर pattern branch तब तक order में try होता है जब तक एक match न हो जाए।

उदाहरण: Using case for Substring Checks

bash
#!/bin/bash
url="https://example.com/page"
case $url in
    https://*)
        echo "Secure URL"
        ;;
    http://*)
        echo "Insecure URL"
        ;;
    *)
        echo "Unknown URL scheme"
        ;;
esac

Quoted Patterns Disable Glob Matching

अगर [[ ]] के अंदर == के right वाला pattern quoted हो, Bash इसे एक glob pattern के बजाय एक literal string मानता है, इसलिए * जैसे wildcards अपना special meaning खो देते हैं और exactly match करने वाले ordinary characters बन जाते हैं।

यह distinction याद रखना important है क्योंकि यह check को contains से 'is exactly equal to' में बदल देता है।

उदाहरण: Quoted Patterns Disable Glob Matching

bash
#!/bin/bash
value="hello"
if [[ $value == h* ]]; then
    echo "Unquoted pattern: glob match, starts with 'h'"
fi
if [[ $value == "h*" ]]; then
    echo "This won't print"
else
    echo "Quoted pattern: treated as literal 'h*', no match"
fi
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. [[ $str == *substring* ]] pattern को quotes में उपयोग करने की कोशिश करना; pattern को quote करना glob matching को पूरी तरह off कर देता है और इसके बजाय एक literal, exact-string comparison मजबूर करता है।
  2. यह मान लेना कि case और [[ == ]] ^/$ जैसे regex-style anchors support करते हैं; वे glob wildcards (*, ?, [...]) उपयोग करते हैं, जहां * implicitly पहले से मतलब रखता है 'उस position पर कुछ भी, बिना कुछ के भी, कहीं भी'।
  3. [[ $str = *sub* ]] (single =) उपयोग करना और यह देख कर surprised होना कि यह भी glob matching करता है; [[ ]] के अंदर, = और == दोनों pattern matching के लिए identically behave करते हैं, [ ] के अंदर के विपरीत जहां = strictly एक literal string comparison है।
चैप्टर सारांश
  • [[ $str == *substring* ]] जांचता है कि $str के अंदर कहीं substring आता है या नहीं, unquoted glob wildcards उपयोग करके।
  • [[ $str == prefix* ]] जांचता है कि $str prefix से शुरू होता है या नहीं; [[ $str == *suffix ]] जांचता है कि यह suffix पर खत्म होता है या नहीं।
  • case pattern branches के साथ वही checks achieve कर सकता है, जो कई possible substrings एक साथ check करते समय अच्छा पढ़ता है।
  • Pattern को quote करना (जैसे "*substring*") glob matching disable करता है और एक literal, exact-string comparison मजबूर करता है -- यहां आप usually जो चाहते हैं उसका opposite।

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.