shell中的各种括号的使用方法

2019-09-23 09:29:49王振洲

              string.   If  pattern  begins with /, all matches of pattern are
              replaced  with  string.   Normally  only  the  first  match   is
              replaced.  If pattern begins with #, it must match at the begin‐
              ning of the expanded value of parameter.  If pattern begins with
              %,  it must match at the end of the expanded value of parameter.
              If string is null, matches of pattern are deleted and the / fol‐
              lowing pattern may be omitted.  If parameter is @ or *, the sub‐
              stitution operation is applied to each positional  parameter  in
              turn,  and the expansion is the resultant list.  If parameter is
              an array variable subscripted with  @  or  *,  the  substitution
              operation  is  applied  to each member of the array in turn, and
              the expansion is the resultant list.

(( )) :一对圆括号有两个地方用到。

1,for循环,

for (( expr1 ; expr2 ; expr3 ))

这里一对双括号里边的表达式,GNU的文档指出,expr1支持 Shell Arithmetic;expr2不为0时,expr3被赋值且语句执行。说的很麻烦,还要花时间搞清楚什么是Shell Arithmetic。其实一言以蔽之,支持数字条件。比如:

for (( a=0 ; a<10 ; a++ )); do echo $a; done

会输出 0 1 2 3 (带换行哦~~~)

2,数学表达

(( )) 和 $(( ))

(( )) 的用法与let一样,就不用多解释了吧~~~

$(( ))就是把计算结果拿出来,可以用在双引号里边,比如:

echo "1+2=$(( 1 + 2 ))"

会输出 1+2=3

( ):一个圆括号

在for循环里,跟C语法一样一样的。

或者是子程序,返回整个里边表达的返回值。里边的变量都是局部的,修改不会带到外边。举例子

a=1

(a=3; echo $a)

echo a

结果是 3 1

还有个就是圈数组。。。这个就没神马意思了

[ ]:一个方括号,是bash的命令,查man手册是可以查到的,跟test一样,在手册里可以看到很多用法。比如-b -c -gt -eq 什么的很多,还有用-a表示与,-o表示或等等