[[ ]] Extended Test
The double-bracket test is like a smarter version of the quiz question: it copes with empty answers, matches patterns and does not get confused by spaces in your words.
In this page:
Syntax
[[ $string =~ regex ]]
[[ $string == pattern* ]]
[[ condition1 && condition2 ]]
[[ ]] Extended Test
[[ ]] is Bash's extended conditional syntax, supporting pattern matching, regex with =~, and safer handling of unquoted variables than [ ].
It doesn't word-split or glob-expand its operands, avoiding many of [ ]'s footguns. Modern Bash scripts generally prefer [[ ]] over [ ] when portability to POSIX sh isn't required.
Note:
[[ $str =~ ^[0-9]+$ ]] tests whether a string matches a regular expression — [ ] can't do this.Example: [[ ]] 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. #}
Common Mistakes
- Quoting the pattern on the right of
=~, which makes it a plain string instead of a regex. - Using
[[ ]]in a script that runs withsh, where it may not exist. - Expecting
&&and||to work inside[ ], when they work in[[ ]].
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: