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

[[ ]] Extended Test

In this page:

  1. [[ ]] Extended Test

[[ ]] 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

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

if [[ $value =~ ^[0-9]+$ ]]; then
    echo "$value is numeric"
else
    echo "$value is not numeric"
fi

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.