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

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

Double-bracket test quiz question के एक smarter version जैसा है: यह empty answers से cope करता है, patterns match करता है और आपके words में spaces से confused नहीं होता।

In this page:

  1. [[ ]] Extended Test
Syntax
bash
[[ $string =~ regex ]]
[[ $string == pattern* ]]
[[ condition1 && condition2 ]]

[[ ]] Extended Test

[[ ]] Bash का extended conditional syntax है, pattern matching, =~ के साथ regex, और [ ] से safer unquoted variables की handling support करते हुए।

यह अपने operands को word-split या glob-expand नहीं करता, [ ] के कई footguns से बचते हुए। Modern Bash scripts आमतौर पर [[ ]] को [ ] से ज़्यादा पसंद करती हैं जब POSIX sh में portability की ज़रूरत न हो।

Note: [[ $str =~ ^[0-9]+$ ]] test करता है कि एक string एक regular expression से match करता है या नहीं — [ ] यह नहीं कर सकता।

उदाहरण: [[ ]] Extended Test

bash
#!/bin/bash
value="12345"

if [[ $value =~ ^[0-9]+$ ]]; then
    echo "$value is numeric"
else
    echo "$value is not numeric"
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. =~ के right side पर pattern को quote करना, जो इसे एक plain string बना देता है regex के बजाय।
  2. sh से run होने वाली एक script में [[ ]] उपयोग करना, जहां यह exist नहीं कर सकता।
  3. यह उम्मीद करना कि && और || [ ] में काम करें, जब वे [[ ]] में काम करते हैं।

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.