Bash中的圆括号/中括号/大括号


圆括号 -> Parenthesis: ()
中括号 -> Brackets: []  #也叫做“square brackets”
大括号 -> Braces: {}   #也叫做“curly braces”

搜索关键字:
  • bash bracket parenthesis brace
  • bash square brackets
参考链接:
==Bash中的“[”和“[[”的区别==
参考解答:

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

双方括号 [[ 可以提供更多的功能,比如:你可以使用&&和||替代-a和-o,以及使用 =~ 进行正则匹配。

大括号除了可以限定变量名进行参数扩展,还可以用来进行如下操作:

截取变量的部分内容

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

和sed一样进行内容替换

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

设置默认值

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

还有就是,大括号扩展可以创建字符串列表用于进行循环迭代:

$ 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}  #只在Bash 4之后可用
00 02 04 06 08

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

 

双圆括号 (( )) 一般用于算术运算

((a++))
((meaning = 42))
for ((i=0; i<10; i++))
echo $((a + b + (14 * c)))

在 (( )) 内部允许整数和数组变量省略$符号,还可以在操作符之间添加空格以增加可读性

单方括号也可用于标识数组下标

array[4]="hello"
element=${array[4]}

圆括号 () 可用于创建子shell,也可用于创建数组:

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

==

$ VARIABLE=abcdef
$ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
yes

$ type [
[ is a shell builtin
$ VARIABLE=abcdef
$ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi
no

$ type [[
[[ is a shell keyword
$ pwd
/home/user
$ (cd /tmp; pwd)
/tmp
$ pwd
/home/user

从上面的示例中可以看出,用 () 创建的子shell可以在不影响当前环境的情况下进行操作

大括号 {} 用于明确标识变量:

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

大括号 {} 同样可以用来在当前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

与 () 不同的是,在使用 {} 时,最后一个命令必须要带上一个分号,以及在 { 和 } 两端需要有空格

==

方括号 -> Brackets
if [ CONDITION ]    Test construct
if [[ CONDITION ]]  Extended test construct
Array[1]=element1   Array initialization
[a-z]               Range of characters within a Regular Expression
大括号 -> Curly Brackets
${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

=EOF=


《“Bash中的圆括号/中括号/大括号”》 有 1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注