在 Bash 中,双方括号 [[]] 比单方括号 [] 更可取吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/669452/
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
Is double square brackets [[ ]] preferable over single square brackets [ ] in Bash?
提问by Leonard
A co-worker claimed recently in a code review that the [[ ]]
construct is to be preferred over [ ]
in constructs like
一位同事最近在一次代码中声称,该[[ ]]
构造优于[ ]
类似的构造
if [ "`id -nu`" = "$someuser" ] ; then
echo "I love you madly, $someuser"
fi
He couldn't provide a rationale. Is there one?
他无法提供理由。有吗?
回答by Johannes Schaub - litb
[[
has fewer surprises and is generally safer to use. But it is not portable - POSIX doesn't specify what it does and only some shells support it (beside bash, I heard ksh supports it too). For example, you can do
[[
有更少的惊喜,通常使用起来更安全。但它不是可移植的——POSIX 没有指定它做什么,只有一些 shell 支持它(除了 bash,我听说 ksh 也支持它)。例如,你可以做
[[ -e $b ]]
to test whether a file exists. But with [
, you have to quote $b
, because it splits the argument and expands things like "a*"
(where [[
takes it literally). That has also to do with how [
can be an external program and receives its argument just normally like every other program (although it can also be a builtin, but then it still has not this special handling).
测试文件是否存在。但是对于[
,您必须引用$b
,因为它会拆分参数并扩展诸如"a*"
([[
字面意思)之类的内容。这也与如何[
成为外部程序并像其他程序一样正常接收其参数有关(尽管它也可以是内置程序,但它仍然没有这种特殊处理)。
[[
also has some other nice features, like regular expression matching with =~
along with operators like they are known in C-like languages. Here is a good page about it: What is the difference between test, [
and [[
?and Bash Tests
[[
还具有其他一些不错的功能,例如正则表达式匹配=~
以及类似 C 语言中已知的运算符。这是一个很好的页面:test[
和[[
? 和Bash 测试
回答by Leonard
[[ ]]
has more features - I suggest you take a look at the Advanced Bash Scripting Guidefor more info, specifically the extended test commandsection in Chapter 7. Tests.
[[ ]]
具有更多功能 - 我建议您查看高级 Bash 脚本指南以获取更多信息,特别是第 7 章中的扩展测试命令部分。测试。
Incidentally, as the guide notes, [[ ]]
was introduced in ksh88 (the 1988 version of the Korn shell).
顺便提一下,如指南所述,[[ ]]
是在 ksh88(Korn shell 的 1988 版本)中引入的。
回答by f3lix
From Which comparator, test, bracket, or double bracket, is fastest?(http://bashcurescancer.com)
从哪个比较器、测试、括号或双括号中最快?( http://bashcurescancer.com)
The double bracket is a “compound command” where as test and the single bracket are shell built-ins (and in actuality are the same command). Thus, the single bracket and double bracket execute different code.
The test and single bracket are the most portable as they exist as separate and external commands. However, if your using any remotely modern version of BASH, the double bracket is supported.
双括号是一个“复合命令”,其中 test 和单括号是 shell 内置命令(实际上是相同的命令)。因此,单括号和双括号执行不同的代码。
test 和单括号是最便携的,因为它们作为单独的外部命令存在。但是,如果您使用 BASH 的任何远程现代版本,则支持双括号。
回答by crizCraig
If you are into following Google's style guide:
如果您想遵循Google 的风格指南:
Test, [
and [[
测试,[
和[[
[[ ... ]]
reduces errors as no path name expansion or word splitting takes place between[[
and]]
, and[[ ... ]]
allows for regular expression matching where[ ... ]
does not.
[[ ... ]]
减少错误,因为在[[
和之间没有发生路径名扩展或分词]]
,并[[ ... ]]
允许正则表达式匹配[ ... ]
没有的地方。
# This ensures the string on the left is made up of characters in the
# alnum character class followed by the string name.
# Note that the RHS should not be quoted here.
# For the gory details, see
# E14 at https://tiswww.case.edu/php/chet/bash/FAQ
if [[ "filename" =~ ^[[:alnum:]]+name ]]; then
echo "Match"
fi
# This matches the exact pattern "f*" (Does not match in this case)
if [[ "filename" == "f*" ]]; then
echo "Match"
fi
# This gives a "too many arguments" error as f* is expanded to the
# contents of the current directory
if [ "filename" == f* ]; then
echo "Match"
fi
回答by Vicente Bolea
A typical situation where you cannot use [[
is in an autotools configure.ac script, there brackets has a special and different meaning, so you will have to use test
instead of [
or [[
-- Note that test and [
are the same program.
不能使用的典型情况[[
是在 autotools configure.ac 脚本中,括号具有特殊且不同的含义,因此您必须使用ortest
代替-- 注意,test 和是同一个程序。[
[[
[
回答by scavenger
[[ ]] double brackets are unsuported under certain version of SunOS and totally unsuported inside function declarations by : GNU bash, version 2.02.0(1)-release (sparc-sun-solaris2.6)
[[ ]] 在某些版本的 SunOS 下不支持双括号,并且完全不支持内部函数声明:GNU bash,版本 2.02.0(1)-release (sparc-sun-solaris2.6)
回答by unix4linux
In a nutshell, [[ is better because it doesn't fork another process. No brackets or a single bracket is slower than a double bracket because it forks another process.
简而言之, [[ 更好,因为它不会分叉另一个进程。没有括号或单个括号比双括号慢,因为它派生了另一个进程。