[[ ]] Extended Test का उपयोग
Double-bracket test quiz question के एक smarter version जैसा है: यह empty answers से cope करता है, patterns match करता है और आपके words में spaces से confused नहीं होता।
In this page:
Syntax
[[ $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
#!/bin/bash
value="12345"
if [[ $value =~ ^[0-9]+$ ]]; then
echo "$value is numeric"
else
echo "$value is not numeric"
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. #}
आम गलतियां
=~के right side पर pattern को quote करना, जो इसे एक plain string बना देता है regex के बजाय।shसे run होने वाली एक script में[[ ]]उपयोग करना, जहां यह exist नहीं कर सकता।- यह उम्मीद करना कि
&&और||[ ]में काम करें, जब वे[[ ]]में काम करते हैं।
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: