[[ ]] Extended Test
In this page:
[[ ]] 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
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: