Substrings/Prefixes जाँचना
case या glob wildcards के साथ [[ ]] test उपयोग करके।In this page:
[[ $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
#!/bin/bash
sentence="The quick brown fox jumps"
if [[ $sentence == *brown* ]]; then
echo "Found 'brown' in the sentence"
fi
Login to try C/C++/Java/PHP code in the editor
Checking a Prefix or Suffix
[[ $str == prefix* ]] जांचता है कि $str prefix से शुरू होता है, और [[ $str == *suffix ]] जांचता है कि यह suffix पर खत्म होता है, wildcard को सिर्फ उस side पर रखकर जिसकी आपको परवाह नहीं।
यह naturally पढ़ता है और शुरुआत या आखिर compare करने के लिए manual substring slicing से बचाता है।
उदाहरण: Checking a Prefix or Suffix
#!/bin/bash
filename="report_2024.csv"
if [[ $filename == report_* ]]; then
echo "Starts with 'report_'"
fi
if [[ $filename == *.csv ]]; then
echo "Ends with '.csv'"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
url="https://example.com/page"
case $url in
https://*)
echo "Secure URL"
;;
http://*)
echo "Insecure URL"
;;
*)
echo "Unknown URL scheme"
;;
esac
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
[[ $str == *substring* ]]pattern को quotes में उपयोग करने की कोशिश करना; pattern को quote करना glob matching को पूरी तरह off कर देता है और इसके बजाय एक literal, exact-string comparison मजबूर करता है।- यह मान लेना कि
caseऔर[[ == ]]^/$जैसे regex-style anchors support करते हैं; वे glob wildcards (*,?,[...]) उपयोग करते हैं, जहां*implicitly पहले से मतलब रखता है 'उस position पर कुछ भी, बिना कुछ के भी, कहीं भी'। [[ $str = *sub* ]](single=) उपयोग करना और यह देख कर surprised होना कि यह भी glob matching करता है;[[ ]]के अंदर,=और==दोनों pattern matching के लिए identically behave करते हैं,[ ]के अंदर के विपरीत जहां=strictly एक literal string comparison है।
[[ $str == *substring* ]]जांचता है कि$strके अंदर कहींsubstringआता है या नहीं, unquoted glob wildcards उपयोग करके।[[ $str == prefix* ]]जांचता है कि$strprefixसे शुरू होता है या नहीं;[[ $str == *suffix ]]जांचता है कि यहsuffixपर खत्म होता है या नहीं।casepattern branches के साथ वही checks achieve कर सकता है, जो कई possible substrings एक साथ check करते समय अच्छा पढ़ता है।- Pattern को quote करना (जैसे
"*substring*") glob matching disable करता है और एक literal, exact-string comparison मजबूर करता है -- यहां आप usually जो चाहते हैं उसका opposite।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: