Linux command
double-bracket 命令
文件
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Test condition (bash/zsh extended)
[[ -f [file] ]] && echo "exists"
Pattern matching
[[ "[string]" == pattern* ]]
Regex matching
[[ "[string]" =~ ^[0-9]+$ ]]
Safe variable comparison (no quoting needed)
[[ $var == "value" ]]
Combine conditions
[[ -f [file] && -r [file] ]]
说明
[[ is bash/zsh's enhanced conditional expression. It provides safer and more powerful tests than [. Key advantages over [: - No word splitting on variables (unquoted $var is safe) - Pattern matching with == and != - Regular expression matching with =~ - && and || work inside the brackets - < and > don't need escaping ```bash
FAQ
What is the double-bracket command used for?
[[ is bash/zsh's enhanced conditional expression. It provides safer and more powerful tests than [. Key advantages over [: - No word splitting on variables (unquoted $var is safe) - Pattern matching with == and != - Regular expression matching with =~ - && and || work inside the brackets - < and > don't need escaping ```bash
How do I run a basic double-bracket example?
Run `[[ -f [file] ]] && echo "exists"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more double-bracket examples?
This page includes 5 examples for double-bracket, plus related commands for nearby Linux tasks.