1. Parentheses, parentheses ()
1. Single parentheses ()
①Command group. Commands in parentheses will be executed sequentially in a new subshell, so variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons. The last command can be without a semicolon, and there is no need to have spaces between each command and the parentheses.
②Command substitution. Equivalent to `cmd`, the shell scans the command line once and finds the $(cmd) structure, executes the cmd in $(cmd) once, gets its standard output, and puts this output into the original command. Some shells do not support it, such as tcsh.
③ used to initialize the array. Such as: array=(abcd)
2. Double parentheses (( ))
①Integer expansion. This extended calculation is an integer calculation and does not support floating point. The ((exp)) construct expands and evaluates an arithmetic expression, returning an exit status code of 1 if the result of the expression is 0, or "false", and a non-zero value returned by an expression The exit status code will be 0, or "true". If it is a logical judgment, the expression exp is 1 if it is true, and 0 if it is false.
②As long as the operators and expressions in parentheses conform to the C language operation rules, they can be used in $((exp)), even ternary operators. When performing operations with different carry (such as binary, octal, and hexadecimal), the output results are all automatically converted into decimal. For example: echo $((16#5f)) The result is 95 (hexadecimal to decimal)
③ Simply use (( )) to redefine variable values, such as a=5; ((a++)) can redefine $a to 6
④Commonly used for arithmetic operation comparison, the variable in double brackets can not use the $ sign prefix. Multiple expressions within parentheses are supported separated by commas. As long as the expressions in the parentheses conform to the C language operation rules, for example, you can use for((i=0;i<5;i++)) directly, if you do not use double brackets, it is for i in `seq 0 4` or for i in {0..4}.For another example, if (($i<5)) can be used directly, if double brackets are not used, it is if [ $i -lt 5 ].
Two, square brackets, square brackets []
1. Single brackets []
①The internal command of bash, [ and test are equivalent. If we don't specify the absolute path, we usually use the commands that come with bash. The left square bracket in the if/test structure is the command identifier for calling test, and the right square bracket is used to close the conditional judgment. This command takes its argument as a comparison expression or as a file test, and returns an exit status code based on the result of the comparison. The closing bracket is not required in the if/test structure, but it is required in the new version of Bash.
②The only comparison operators available in Test and [] are == and !=, both of which are used for string comparison, but not for integer comparison. Integer comparison can only use the form of -eq, -gt. Neither string comparisons nor integer comparisons support the greater-than sign and the less-than sign. If you really want to use it, you can use the escaped form for string comparison. If you compare "ab" and "bc": [ ab \< bc ], the result is true, that is, the return status is 0. Logical AND and logical OR in [ ] are expressed using -a and -o.
③ character range. Used as part of a regular expression to describe a range of characters to match. Regular expressions cannot be used within square brackets for test purposes.
④ In the context of an array structure, square brackets are used to refer to the number of each element in the array.
2. Double brackets [[ ]]
①[[ is a keyword in the bash programming language. Not a command, the [[ ]] construct is more general than the [ ] construct. All characters between [[ and ]] do not have filename expansion or word splitting, but parameter expansion and command substitution will occur.
② Supports pattern matching of strings, and even supports regular expressions of the shell when using the =~ operator. String comparisons can use the right-hand side as a pattern, not just a string, such as [[ hello == hell? ]], which results in true. [[ ]] matches strings or wildcards without quotes.
③使用[[ ... ]]条件判断结构,而不是[ ... ],能够防止脚本中的许多逻辑错误。比如,&&、||、<和> 操作符能够正常存在于[[ ]]条件判断结构中,但是如果出现在[ ]结构中的话,会报错。比如可以直接使用if [[ $a != 1 && $a != 2 ]], 如果不适用双括号, 则为if [ $a -ne 1] && [ $a != 2 ]或者if [ $a -ne 1 -a $a != 2 ]。
④bash把双中括号中的表达式看作一个单独的元素,并返回一个退出状态码。
例子:
例子:
- if ($i<5)
- if [ $i -lt 5 ]
- if [ $a -ne 1 -a $a != 2 ]
- if [ $a -ne 1] && [ $a != 2 ]
- if [[ $a != 1 && $a != 2 ]]
- for i in $(seq 0 4);do echo $i;done
- for i in `seq 0 4`;do echo $i;done
- for ((i=0;i<5;i++));do echo $i;done
- for i in {0..4};do echo $i;done
三、大括号、花括号 {}
1、常规用法
①大括号拓展。(通配(globbing))将对大括号中的文件名做扩展。在大括号中,不允许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展作用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt
- # ls {ex1,ex2}.sh
- ex1.sh ex2.sh
- # ls {ex{1..3},ex4}.sh
- ex1.sh ex2.sh ex3.sh ex4.sh
- # ls {ex[1-3],ex4}.sh
- ex1.sh ex2.sh ex3.sh ex4.sh
②代码块,又被称为内部组,这个结构事实上创建了一个匿名函数 。与小括号中的命令不同,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必须要有一个空格。
2、几种特殊的替换结构
${var:-string},${var:+string},${var:=string},${var:?string}
①${var:-string}和${var:=string}:若变量var为空,则用在命令行中用string来替换${var:-string},否则变量var不为空时,则用变量var的值来替换${var:-string};对于${var:=string}的替换规则和${var:-string}是一样的,所不同之处是${var:=string}若var为空时,用string替换${var:=string}的同时,把string赋给变量var: ${var:=string}很常用的一种用法是,判断某个变量是否赋值,没有的话则给它赋上一个默认值。
② ${var:+string}的替换规则和上面的相反,即只有当var不是空的时候才替换成string,若var为空时则不替换或者说是替换成变量 var的值,即空值。(因为变量var此时为空,所以这两种说法是等价的)
③${var:?string}替换规则为:若变量var不为空,则用变量var的值来替换${var:?string};若变量var为空,则把string输出到标准错误中,并从脚本中退出。我们可利用此特性来检查是否设置了变量的值。
补充扩展:在上面这五种替换结构中string不一定是常值的,可用另外一个变量的值或是一种命令的输出。
② ${var:+string}的替换规则和上面的相反,即只有当var不是空的时候才替换成string,若var为空时则不替换或者说是替换成变量 var的值,即空值。(因为变量var此时为空,所以这两种说法是等价的)
③${var:?string}替换规则为:若变量var不为空,则用变量var的值来替换${var:?string};若变量var为空,则把string输出到标准错误中,并从脚本中退出。我们可利用此特性来检查是否设置了变量的值。
补充扩展:在上面这五种替换结构中string不一定是常值的,可用另外一个变量的值或是一种命令的输出。
3、四种模式匹配替换结构
模式匹配记忆方法:
# 是去掉左边(在键盘上#在$之左边)
% 是去掉右边(在键盘上%在$之右边)
#和%中的单一符号是最小匹配,两个相同符号是最大匹配。
${var%pattern},${var%%pattern},${var#pattern},${var##pattern}
第一种模式:${variable%pattern},这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最短的匹配模式第二种模式: ${variable%%pattern},这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最长的匹配模式
第三种模式:${variable#pattern} 这种模式时,shell在variable中查找,看它是否一给的模式pattern开始,如果是,就从命令行把variable中的内容去掉左边最短的匹配模式
第四种模式: ${variable##pattern} 这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最长的匹配模式
这四种模式中都不会改变variable的值,其中,只有在pattern中使用了*匹配符号时,%和%%,#和##才有区别。结构中的pattern支持通配符,*表示零个或多个任意字符,?表示仅与一个任意字符匹配,[...]表示匹配中括号里面的字符,[!...]表示不匹配中括号里面的字符 。
- # var=testcase
- # echo $var
- testcase
- # echo ${var%s*e}
- testca
- # echo $var
- testcase
- # echo ${var%%s*e}
- te
- # echo ${var#?e}
- stcase
- # echo ${var##?e}
- stcase
- # echo ${var##*e}
- # echo ${var##*s}
- e
- # echo ${var##test}
- case
4、字符串提取和替换
${var:num},${var:num1:num2},${var/pattern/pattern},${var//pattern/pattern}
第一种模式:${var:num},这种模式时,shell在 var中提取第num个字符到末尾的所有字符。若num为正数,从左边0处开始;若num为负数,从右边开始提取字串,但必须使用在冒号后面加空格或一个数字或整个num加上括号,如${var: -2}、${var:1-3}或${var:(-2)}。第二种模式:${var:num1:num2},num1是位置,num2是长度。表示从$var字符串的第$num1个位置开始提取长度为$num2的子串。不能为负数。
第三种模式:${var/pattern/pattern}表示将var字符串的第一个匹配的pattern替换为另一个pattern。。
第四种模式:${var//pattern/pattern}表示将var字符串中的所有能匹配的pattern替换为另一个pattern。
- [root@centos ~]# var=/home/centos
- [root@centos ~]# echo $var
- /home/centos
- [root@centos ~]# echo ${var:5}
- /centos
- [root@centos ~]# echo ${var: -6}
- centos
- [root@centos ~]# echo ${var:(-6)}
- centos
- [root@centos ~]# echo ${var:1:4}
- home
- [root@centos ~]# echo ${var/o/h}
- /hhme/centos
- [root@centos ~]# echo ${var//o/h}
- /hhme/cenths
四、符号$后的括号
(1)${a} 变量a的值, 在不引起歧义的情况下可以省略大括号。
(2)$(cmd) 命令替换,和`cmd`效果相同,结果为shell命令cmd的输,过某些Shell版本不支持$()形式的命令替换, 如tcsh。
(3)$((expression)) 和`exprexpression`效果相同, 计算数学表达式exp的数值, 其中exp只要符合C语言的运算规则即可, 甚至三目运算符和逻辑表达式都可以计算。
五、使用
1、多条命令执行
(1)单小括号,(cmd1;cmd2;cmd3) 新开一个子shell顺序执行命令cmd1,cmd2,cmd3, 各命令之间用分号隔开, 最后一个命令后可以没有分号。
(2)单大括号,{ cmd1;cmd2;cmd3;} 在当前shell顺序执行命令cmd1,cmd2,cmd3, 各命令之间用分号隔开, 最后一个命令后必须有分号, 第一条命令和左括号之间必须用空格隔开。
对{}和()而言, 括号中的重定向符只影响该条命令, 而括号外的重定向符影响到括号中的所有命令。