String की तुलना
String comparison यह check करने जैसा है कि दो name tags में एक ही नाम लिखा है या नहीं। Bash यह भी test कर सकता है कि कोई word किसी pattern में fit होता है या नहीं, जैसे 'A से शुरू होता है'।
In this page:
Syntax
[[ "$string1" == "$string2" ]]
[[ "$string1" != "$string2" ]]
[[ "$string1" < "$string2" ]]
String Comparison
[[ ]] के अंदर, strings को == (equal), != (not equal), और lexicographically </> से compare किया जाता है। [[ ]] right side पर एक glob pattern के against == से pattern matching भी support करता है।
String comparison पहले cover किए गए numeric -eq/-lt operators से अलग है।
Warning:
Strings को
-eq जैसे numeric operators से compare करना गलत या error results देता है — strings के लिए हमेशा ==/!= उपयोग करें।उदाहरण: String Comparison
#!/bin/bash
a="apple"
b="banana"
if [[ "$a" == "$b" ]]; then
echo "Equal"
else
echo "Not equal"
fi
if [[ "$a" == a* ]]; then
echo "$a starts with 'a'"
fi
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
[ ]के अंदर unquoted variables के साथ=या==उपयोग करना, जो तब टूटता है जब value empty हो या spaces हों।- Strings के लिए
-eqउपयोग करना बजाय=के। =के आस-पास spaces भूल जाना, जैसे[ "$a"="$b" ], जो हमेशा true होता है।
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: