[] 在 bash 中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11796751/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does [] do in bash?
提问by Andriy Drozdyuk
Possible Duplicate:
bash: double or single bracket, parentheses, curly braces
可能的重复:
bash:双括号或单括号、圆括号、花括号
Looking at the rc.dcron script in archlinux:
查看rc.darchlinux中的cron脚本:
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
name=crond
. /etc/conf.d/crond
PID=$(pidof -o %PPID /usr/sbin/crond)
case "" in
start)
    stat_busy "Starting $name daemon"
    [[ -z "$PID" ]] && /usr/sbin/crond $CRONDARGS &>/dev/null \
    && { add_daemon $name; stat_done; } \
    || { stat_fail; exit 1; }
    ;;
While I can figure out most of the syntax, what the heck does this do:
虽然我可以弄清楚大部分语法,但这到底是做什么的:
 [[ -z "$PID" ]]
I saw that also written as:
我看到也写成:
 [ -z "$PID" ]
In reference I found that []is used in if-statements, but I see none here. Any help is much appreciated. Thanks!
在参考中,我发现[]在 if 语句中使用了它,但我在这里没有看到。任何帮助深表感谢。谢谢!
采纳答案by Mithrandir
The opening bracket ([) is an alias for the test command which performs all the tests and returns 0 for true or something else for false. The "if" reacts only to the return value of the test command. The closing bracket tells test where the expression ends. The double brackets ([[) are a bash built in and can replace the external call to test.
左括号 ([) 是 test 命令的别名,它执行所有测试并返回 0 表示真或其他内容表示假。“if”仅对测试命令的返回值作出反应。右括号告诉 test 表达式在哪里结束。双括号 ([[) 是内置的 bash,可以代替外部调用 test。
回答by Colonel Panic
The single brackets emulate /usr/bin/test, an old Unix utility. They do what you ask for, but not what you want. The double brackets are improvement, peculiar to Bash.
单括号模拟/usr/bin/test,一个旧的 Unix 实用程序。他们做你要求的,但不是你想要的。双括号是改进的,是 Bash 特有的。
http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS
http://tldp.org/LDP/abs/html/testconstructs.html#DBLBBRACKETS
There exists a dedicated command called [ (left bracket special character). It is a synonym for
test, and a builtin for efficiency reasons. This command considers its arguments as comparison expressions or file tests and returns an exit status corresponding to the result of the comparison (0 for true, 1 for false).With version 2.02, Bash introduced the [[ ... ]] extended test command, which performs comparisons in a manner more familiar to programmers from other languages. Note that [[ is a keyword, not a command.
No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and command substitution.
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
存在一个名为 [(左括号特殊字符)的专用命令。它是 的同义词
test,并且是出于效率原因的内置函数。此命令将其参数视为比较表达式或文件测试,并返回与比较结果相对应的退出状态(0 表示真,1 表示假)。在 2.02 版中,Bash 引入了 [[ ... ]] 扩展测试命令,它以其他语言的程序员更熟悉的方式执行比较。请注意, [[ 是关键字,而不是命令。
[[和]]之间没有文件名扩展或分词,但有参数扩展和命令替换。
使用 [[ ... ]] 测试结构而不是 [ ... ] 可以防止脚本中的许多逻辑错误。例如, &&、||、< 和 > 运算符在 [[ ]] 测试中工作,尽管在 [ ] 构造中给出错误。
回答by Daniel O'Hara
From thismanual:
从本手册:
Brackets to return a binary result of expression: [[ ]]
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression. Word splitting and filename expansion are not performed on the words between the
[[' and]]'; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.The && and || commands do not execute expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.
括号返回表达式的二进制结果:[[]]
[[ 表达 ]]
根据条件表达式的计算结果返回 0 或 1 状态。
[[' and]]'之间的单词不进行分词和文件名扩展;执行波浪号扩展、参数和变量扩展、算术扩展、命令替换、进程替换和引号删除。&& 和 || 如果 expression1 的值足以确定整个条件表达式的返回值,则命令不会执行 expression2。

