← Back to Bash Course | Chapter 3: Control Flow | Lesson 6 of 14

[[ ]] Extended Test Operator का उपयोग

[[ ]] [ ] का Bash का improved version है जो quoting के बारे में ज़्यादा forgiving है और pattern matching और regular expressions जैसे extra features जोड़ता है।
Syntax
bash
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

bash
#!/bin/bash
name=""
if [[ $name = "" ]]; then
    echo "name is empty, and this worked without quoting"
fi

[[ ]] के अंदर Logical Operators

[[ ]] double brackets के अंदर सीधे && और || को multiple conditions को एक expression में combine करने की अनुमति देता है, जो [ ] के -a/-o (जो deprecated भी हैं) पर reliance से cleaner है। यह compound conditions को कहीं ज़्यादा readable बनाता है।

उदाहरण: Logical Operators Inside [[ ]]

bash
#!/bin/bash
age=25
has_id=true
if [[ $age -ge 18 && $has_id = true ]]; then
    echo "Entry allowed"
fi

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 ==

bash
#!/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

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 =~

bash
#!/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
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. यह मान लेना कि [[ ]] [ ] की तरह पूरी तरह POSIX-portable है; यह एक Bash (और कुछ दूसरी modern shells की) extension है, इसलिए plain sh के under चलने वाले scripts को [ ] से चिपके रहना चाहिए।
  2. यह भूल जाना कि [[ ]] के अंदर, < और > बिना escaping की ज़रूरत के strings को alphabetically compare ही करते हैं ([ ] के अंदर के विपरीत जहां वे redirect करते हैं); numeric comparison को अब भी -lt/-gt आदि चाहिए।
  3. == के 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_REMATCH array में store करते हुए।

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.