[[ ]] Extended Test Operator का उपयोग
[[ ]] [ ] का Bash का improved version है जो quoting के बारे में ज़्यादा forgiving है और pattern matching और regular expressions जैसे extra features जोड़ता है।In this page:
if [[ condition1 && condition2 ]]; then
commands
fi
[[ $string == pattern* ]]
[[ ]] Basics और Safer Quoting
[[ expression ]] basic comparisons के लिए [ ] जैसा behave करता है लेकिन ज़्यादा forgiving है: इसके अंदर unquoted variables word splitting या globbing के subject नहीं होते, जो कई [ ] pitfalls से बचाता है।
यह एक Bash keyword है, एक separate command नहीं, इसलिए इसके parsing rules [ ] से अलग हैं।
उदाहरण: [[ ]] Basics and Safer Quoting
#!/bin/bash
name=""
if [[ $name = "" ]]; then
echo "name is empty, and this worked without quoting"
fi
Login to try C/C++/Java/PHP code in the editor
[[ ]] के अंदर Logical Operators
[[ ]] double brackets के अंदर सीधे && और || को multiple conditions को एक expression में combine करने की अनुमति देता है, जो [ ] के -a/-o (जो deprecated भी हैं) पर reliance से cleaner है। यह compound conditions को कहीं ज़्यादा readable बनाता है।
उदाहरण: Logical Operators Inside [[ ]]
#!/bin/bash
age=25
has_id=true
if [[ $age -ge 18 && $has_id = true ]]; then
echo "Entry allowed"
fi
Login to try C/C++/Java/PHP code in the editor
Glob Pattern Matching with ==
[[ ]] के अंदर, == या != के right side पर एक unquoted pattern को literal string के बजाय एक glob (wildcard) pattern के रूप में match किया जाता है, इसलिए * characters की किसी भी sequence से match करता है।
Pattern को quote करना इस special matching को बंद कर देता है और इसके बजाय एक literal comparison force करता है।
उदाहरण: Glob Pattern Matching with ==
#!/bin/bash
filename="report.txt"
if [[ $filename == *.txt ]]; then
echo "It's a text file"
fi
if [[ $filename == "*.txt" ]]; then
echo "This won't print, because the pattern is quoted (literal)"
fi
Login to try C/C++/Java/PHP code in the editor
Regex Matching with =~
[[ str =~ regex ]] str को एक POSIX extended regular expression के against test करता है, और अगर यह match करे, तो पूरा match और कोई भी parenthesized capture groups BASH_REMATCH array में store होते हैं (BASH_REMATCH[0] पूरा match है, [1] पहला group, आदि)।
Regex को खुद generally portability के लिए unquoted छोड़ा जाना चाहिए या पहले एक variable में store किया जाना चाहिए।
उदाहरण: Regex Matching with =~
#!/bin/bash
email="[email protected]"
if [[ $email =~ ^([a-z]+)@([a-z.]+)$ ]]; then
echo "Full match: ${BASH_REMATCH[0]}"
echo "Username: ${BASH_REMATCH[1]}"
echo "Domain: ${BASH_REMATCH[2]}"
fi
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
[[ ]][ ]की तरह पूरी तरह POSIX-portable है; यह एक Bash (और कुछ दूसरी modern shells की) extension है, इसलिए plainshके under चलने वाले scripts को[ ]से चिपके रहना चाहिए। - यह भूल जाना कि
[[ ]]के अंदर,<और>बिना escaping की ज़रूरत के strings को alphabetically compare ही करते हैं ([ ]के अंदर के विपरीत जहां वे redirect करते हैं); numeric comparison को अब भी-lt/-gtआदि चाहिए। ==के right side पर pattern को glob matching के लिए उपयोग करते समय quote करना, जैसे[[ $file == "*.txt" ]]; quoting इसे wildcard match के बजाय एक literal string comparison में बदल देता है।
[[ ]]इसके अंदर unquoted variables को word-split या glob-expand नहीं करता, इसलिए[[ $name = value ]]बिना quotes के भी safe है।[[ ]]brackets के अंदर सीधे&&और||support करता है,[ ]के विपरीत जिसे-a/-oया separate test invocations चाहिए।[[ str == pattern ]]glob-style wildcard matching support करता है जब right-hand pattern unquoted हो।[[ str =~ regex ]]POSIX extended regular expression matching perform करता है, captured groups कोBASH_REMATCHarray में store करते हुए।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: