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


当前回答

函数定义中的括号

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

function_name () { command1 ; command2 ; }

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

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

$ echo \(
(

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

其他回答

A single bracket ([) usually actually calls a program named [; man test or man [ for more info. Example: $ VARIABLE=abcdef $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi yes 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 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. (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 current shell 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

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

在Bash中,test和[是shell内置的。

双括号是shell关键字,它支持额外的功能。例如,您可以使用&&和||来代替-a和-o,并且有一个正则表达式匹配操作符=~。

此外,在一个简单的测试中,双方括号的计算速度似乎比单方括号快得多。

$ 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

大括号,除了分隔变量名外,还用于参数展开,这样你就可以做以下事情:

截断变量的内容 $ var = "中的";echo $ {var % d *} 美国广播公司 进行类似sed的替换 $ var = "中的";echo $ {var /德/ 12} abc12 使用默认值 默认美元=“你好”;未设置的var;echo $ {var: - $默认} 你好 还有更多

同样,大括号展开创建字符串列表,这些字符串通常在循环中迭代:

$ 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

请注意,前导零和增量特性在Bash 4之前是不可用的。

感谢gboffi提醒我关于大括号展开。

双括号用于算术运算:

((a++))

((meaning = 42))

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

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

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

单括号也用于数组下标:

array[4]="hello"

element=${array[index]}

右边的(most/all?)数组引用需要花括号。

Ephemient的评论提醒了我,括号也用于subshell。它们被用来创建数组。

array=(1 2 3)
echo ${array[1]}
2
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

我只是想从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

test,[和[]之间的区别在BashFAQ中有详细的解释。 (注:链接中有很多例子供比较)

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, and it 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.

结论是:

什么时候使用新的测试命令[[,什么时候使用旧的测试命令[? 如果要考虑到POSIX或BourneShell的可移植性/一致性,则应该考虑旧语法 被使用。如果脚本需要BASH、Zsh或KornShell, 新的语法通常更加灵活。