bash 如何使用双括号或单括号、圆括号、花括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:54:42  来源:igfitidea点击:

How to use double or single brackets, parentheses, curly braces

bashsyntax

提问by Tim

I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?

我对 Bash 中括号、圆括号、花括号的使用以及它们的双重或单一形式之间的区别感到困惑。有明确的解释吗?

采纳答案by Paused until further notice.

In Bash, testand [are shell builtins.

在 Bash 中,test并且[是 shell 内置函数。

The double bracket, which is a shell keyword, enables additional functionality. For example, you can use &&and ||instead of -aand -oand there's a regular expression matching operator =~.

双支架,其是壳关键字,使附加功能。例如,您可以使用&&and||代替-aand-o并且有一个正则表达式匹配 operator =~

Also, in a simple test, double square brackets seem to evaluate quite a lot quicker than single ones.

此外,在一个简单的测试中,双方括号似乎比单方括号评估快得多。

$ time for ((i=0; i<10000000; i++)); do [[ "$i" = 1000 ]]; done

real    0m24.548s
user    0m24.337s
sys 0m0.036s
$ time for ((i=0; i<10000000; i++)); do [ "$i" = 1000 ]; done

real    0m33.478s
user    0m33.478s
sys 0m0.000s

The braces, in addition to delimiting a variable name are used for parameter expansionso you can do things like:

除了分隔变量名外,大括号还用于参数扩展,因此您可以执行以下操作:

  • Truncate the contents of a variable

    $ var="abcde"; echo ${var%d*}
    abc
    
  • Make substitutions similar to sed

    $ var="abcde"; echo ${var/de/12}
    abc12
    
  • Use a default value

    $ default="hello"; unset var; echo ${var:-$default}
    hello
    
  • and several more

  • 截断变量的内容

    $ var="abcde"; echo ${var%d*}
    abc
    
  • 进行类似的替换 sed

    $ var="abcde"; echo ${var/de/12}
    abc12
    
  • 使用默认值

    $ default="hello"; unset var; echo ${var:-$default}
    hello
    
  • 还有几个

Also, brace expansions create lists of strings which are typically iterated over in loops:

此外,大括号扩展创建通常在循环中迭代的字符串列表:

$ echo f{oo,ee,a}d
food feed fad

$ mv error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace expression
expands to "mv error.log error.log.OLD")

$ for num in {000..2}; do echo "$num"; done
000
001
002

$ echo {00..8..2}
00 02 04 06 08

$ echo {D..T..4}
D H L P T

Note that the leading zero and increment features weren't available before Bash 4.

请注意,前导零和增量功能在 Bash 4 之前不可用。

Thanks to gboffi for reminding me about brace expansions.

感谢 gboffi 提醒我大括号扩展。

Double parentheses are used for arithmetic operations:

双括号用于算术运算

((a++))

((meaning = 42))

for ((i=0; i<10; i++))

echo $((a + b + (14 * c)))

and they enable you to omit the dollar signs on integer and array variables and include spaces around operators for readability.

它们使您能够省略整数和数组变量上的美元符号,并在运算符周围包含空格以提高可读性。

Single brackets are also used for arrayindices:

单括号也用于数组索引:

array[4]="hello"

element=${array[index]}

Curly brace are required for (most/all?) array references on the right hand side.

右侧的(大多数/全部?)数组引用需要花括号。

ephemient'scomment reminded me that parentheses are also used for subshells. And that they are used to create arrays.

ephemient 的评论提醒我括号也用于子shell。并且它们用于创建数组。

array=(1 2 3)
echo ${array[1]}
2

回答by Carl Norum

  1. A single bracket ([) usually actually calls a program named [; man testor man [for more info. Example:

    $ VARIABLE=abcdef
    $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
    yes
    
  2. The double bracket ([[) does the same thing (basically) as a single bracket, but is a bash builtin.

    $ VARIABLE=abcdef
    $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi
    no
    
  3. Parentheses (()) are used to create a subshell. For example:

    $ pwd
    /home/user 
    $ (cd /tmp; pwd)
    /tmp
    $ pwd
    /home/user
    

    As you can see, the subshell allowed you to perform operations without affecting the environment of the current shell.

  4. (a) Braces ({}) are used to unambiguously identify variables. Example:

    $ VARIABLE=abcdef
    $ echo Variable: $VARIABLE
    Variable: abcdef
    $ echo Variable: $VARIABLE123456
    Variable:
    $ echo Variable: ${VARIABLE}123456
    Variable: abcdef123456
    

    (b) Braces are also used to execute a sequence of commands in the currentshell context, e.g.

    $ { date; top -b -n1 | head ; } >logfile 
    # 'date' and 'top' output are concatenated, 
    # could be useful sometimes to hunt for a top loader )
    
    $ { date; make 2>&1; date; } | tee logfile
    # now we can calculate the duration of a build from the logfile
    
  1. 单个括号 ( [) 通常实际上调用名为[;的程序。man testman [了解更多信息。例子:

    $ VARIABLE=abcdef
    $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
    yes
    
  2. 双括号 ( [[) 与单括号做同样的事情(基本上),但它是一个内置的 bash。

    $ VARIABLE=abcdef
    $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi
    no
    
  3. 括号 ( ()) 用于创建子外壳。例如:

    $ pwd
    /home/user 
    $ (cd /tmp; pwd)
    /tmp
    $ pwd
    /home/user
    

    如您所见,子shell 允许您在不影响当前shell 环境的情况下执行操作。

  4. (a) 大括号 ( {}) 用于明确标识变量。例子:

    $ VARIABLE=abcdef
    $ echo Variable: $VARIABLE
    Variable: abcdef
    $ echo Variable: $VARIABLE123456
    Variable:
    $ echo Variable: ${VARIABLE}123456
    Variable: abcdef123456
    

    (b) 大括号还用于在当前shell 上下文中执行一系列命令,例如

    $ { date; top -b -n1 | head ; } >logfile 
    # 'date' and 'top' output are concatenated, 
    # could be useful sometimes to hunt for a top loader )
    
    $ { date; make 2>&1; date; } | tee logfile
    # now we can calculate the duration of a build from the logfile
    

There is a subtle syntactic difference with ( ), though (see bash reference) ; essentially, a semicolon ;after the last command within braces is a must, and the braces {, }mustbe surrounded by spaces.

有一个微妙的语法差异( ),虽然(参见bash 参考);本质上,;大括号内最后一个命令后的分号是必须的,并且大括号{,}必须用空格包围。

回答by Yola

Brackets

括号

if [ CONDITION ]    Test construct  
if [[ CONDITION ]]  Extended test construct  
Array[1]=element1   Array initialization  
[a-z]               Range of characters within a Regular Expression
$[ expression ]     A non-standard & obsolete version of $(( expression )) [1]

[1] http://wiki.bash-hackers.org/scripting/obsolete

[1] http://wiki.bash-hackers.org/scripting/obsolete

Curly Braces

大括号

${variable}                             Parameter substitution  
${!variable}                            Indirect variable reference  
{ command1; command2; . . . commandN; } Block of code  
{string1,string2,string3,...}           Brace expansion  
{a..z}                                  Extended brace expansion  
{}                                      Text replacement, after find and xargs

Parentheses

括弧

( command1; command2 )             Command group executed within a subshell  
Array=(element1 element2 element3) Array initialization  
result=$(COMMAND)                  Command substitution, new style  
>(COMMAND)                         Process substitution  
<(COMMAND)                         Process substitution 

Double Parentheses

双括号

(( var = 78 ))            Integer arithmetic   
var=$(( 20 + 5 ))         Integer arithmetic, with variable assignment   
(( var++ ))               C-style variable increment   
(( var-- ))               C-style variable decrement   
(( var0 = var1<98?9:21 )) C-style ternary operation

回答by kzh

I just wanted to add these from TLDP:

我只是想从TLDP添加这些:

~:$ echo $SHELL
/bin/bash

~:$ echo ${#SHELL}
9

~:$ ARRAY=(one two three)

~:$ echo ${#ARRAY}
3

~:$ echo ${TEST:-test}
test

~:$ echo $TEST


~:$ export TEST=a_string

~:$ echo ${TEST:-test}
a_string

~:$ echo ${TEST2:-$TEST}
a_string

~:$ echo $TEST2


~:$ echo ${TEST2:=$TEST}
a_string

~:$ echo $TEST2
a_string

~:$ export STRING="thisisaverylongname"

~:$ echo ${STRING:4}
isaverylongname

~:$ echo ${STRING:6:5}
avery

~:$ echo ${ARRAY[*]}
one two one three one four

~:$ echo ${ARRAY[*]#one}
two three four

~:$ echo ${ARRAY[*]#t}
one wo one hree one four

~:$ echo ${ARRAY[*]#t*}
one wo one hree one four

~:$ echo ${ARRAY[*]##t*}
one one one four

~:$ echo $STRING
thisisaverylongname

~:$ echo ${STRING%name}
thisisaverylong

~:$ echo ${STRING/name/string}
thisisaverylongstring

回答by fwhacking

The difference between test, [and [[is explained in great details in the BashFAQ.

BashFAQ 中详细解释了test[[[之间的区别

To cut a long story short: test implements the old, portable syntax of the command. In almost all shells (the oldest Bourne shells are the exception), [ is a synonym for test (but requires a final argument of ]). Although all modern shells have built-in implementations of [, there usually still is an external executable of that name, e.g. /bin/[.

[[ is a new improved version of it, which is a keyword, not a program. This has beneficial effects on the ease of use, as shown below. [[ is understood by KornShell and BASH (e.g. 2.03), but not by the older POSIX or BourneShell.

长话短说: test 实现了旧的、可移植的命令语法。在几乎所有的 shell 中(最古老的 Bourne shell 是个例外),[ 是 test 的同义词(但需要最后一个参数 ])。尽管所有现代 shell 都有 [ 的内置实现,但通常仍然有该名称的外部可执行文件,例如 /bin/[。

[[是它的新改进版,是关键字,不是程序。这对易用性具有有益影响,如下所示。[[ 被 KornShell 和 BASH(例如 2.03)理解,但不能被旧的 POSIX 或 BourneShell 理解。

And the conclusion:

和结论:

When should the new test command [[ be used, and when the old one [? If portability to the BourneShell is a concern, the old syntax should be used. If on the other hand the script requires BASH or KornShell, the new syntax is much more flexible.

什么时候应该使用新的测试命令 [[ ,什么时候使用旧的 [? 如果考虑到 BourneShell 的可移植性,则应使用旧语法。另一方面,如果脚本需要 BASH 或 KornShell,则新语法要灵活得多。

回答by pabouk

Parentheses in function definition

函数定义中的括号

Parentheses ()are being used in function definition:

()在函数定义中使用 括号:

function_name () { command1 ; command2 ; }

That is the reason you have to escape parentheses even in command parameters:

这就是即使在命令参数中也必须转义括号的原因:

$ echo (
bash: syntax error near unexpected token `newline'

$ echo \(
(

$ echo () { command echo The command echo was redefined. ; }
$ echo anything
The command echo was redefined.

回答by vuppala srikar

Truncate the contents of a variable

$ var="abcde"; echo ${var%d*}
abc

Make substitutions similar to sed

$ var="abcde"; echo ${var/de/12}
abc12

Use a default value

$ default="hello"; unset var; echo ${var:-$default}
hello