double brackets
Bash's double-bracket test, [[ and ]], a keyword that does not split words or expand globs and adds pattern and regular expression matching.
In more detail
An empty variable inside [[ ]] stays an empty string instead of disappearing, so comparisons do not break. == compares against a glob pattern, as in [[ $file == *.txt ]], and =~ against a regular expression. It is not available in POSIX sh.
Example
if [[ "$file" == *.txt ]]; then echo "text file"; fiRelated terms
- test command — The test command, also written with single square brackets, that checks a condition such as a file existing or two values being equal, and exits 0 if it holds.
- reserved word — A word such as if, then, for, do, done, case or function that the shell treats as grammar rather than as a command name.
- glob pattern — A pattern that matches names using * for any run of characters, a question mark for one character and [abc] for one of a set.
- bashism — A bash-only feature, such as [[ ]], arrays, (( )) or process substitution, used in a script that claims to be POSIX sh.
Taught in
- The Unquoted Variable: Splitting And Globbing — Bash Scripting & Command Line Automation
- Exit Codes, And What Success Means — Bash Scripting & Command Line Automation
- Conditionals: test, Brackets, Double Brackets, case — Bash Scripting & Command Line Automation
- Loops: for, while, until, break, continue — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation
- POSIX sh Versus Bash — Bash Scripting & Command Line Automation