模式匹配记忆方法:
# 是去掉左边(在键盘上#在$之左边)
% 是去掉右边(在键盘上%在$之右边)
#和%中的单一符号是最小匹配,两个相同符号是最大匹配。
(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, 各命令之间用分号隔开, 最后一个命令后必须有分号, 第一条命令和左括号之间必须用空格隔开。
对{}和()而言, 括号中的重定向符只影响该条命令, 而括号外的重定向符影响到括号中的所有命令。
例如:
#!/bin/bash
str1="test1"
str2="Test1"
num1=33
num2=4
###################string complare#########
#1 [] use =
if [ "$str1" = "$str2" ]
then
echo "#1 ${str1} equals to ${str2}"
else
echo "#1 ${str1} not e2 $str2"
fi
#2 [] use !=
if [ "$str1" != "$str2" ]
then
echo "#2 ${str1} not equals to ${str2}"
else
echo "#2 ${str1} eq2 $str2"
fi
#3 [] use \< (注意字符串之间不能有空格)
if [ "${str1}"\<"${str2}" ]
then
echo "#3 ${str1} less than ${str2}"
else
echo "#3 ${str1} eq2 or grater than $str2"
fi
#4 [[]] use <
if [[ "$str1" < "$str2" ]]
then
echo "#4 ${str1} less than ${str2}"
else
echo "#4 ${str1} eq2 or grater than $str2"
fi
#################### number complare ############################
#1 [] use -lt.-gt,-ge
if [ "$num1" -lt "${num2}" ]
then
echo "${num1} less than ${num2}"
else
echo "${num1} eq2 or grater than $num2"
fi
#2 [] use \< is complare as string (错误的示范,不能在[]中使用转义比较数字,会当成字符串比较)
if [ "$num1" \< "${num2}" ]
then
echo "${num1} less than ${num2}"
else
echo "${num1} eq2 or grater than $num2"
fi
#3 (()) use <
if (( "$num1" < "${num2}" ))
then
echo "${num1} less than ${num2}"
else
echo "${num1} eq2 or grater than $num2"
fi
#####################(()) use to number +-*/ ###########################
echo "############# (()) use ro number +-*/% ##############"
echo $(($num1 + $num2))
echo $(($num1 - $num2))
echo $(($num1 * $num2))
echo $(($num1 / $num2))
echo $(($num1 % $num2))
####################### () ###################
echo "############# \$() use like \`\` ##################"
echo `which pwd`
echo $(which pwd)
##################### \${} to get variables #########################
echo "################## \${var} is like \$var ################"
echo ${str1}
echo $str1
运行结果:
#1 test1 not e2 Test1
#2 test1 not equals to Test1
#3 test1 less than Test1
#4 test1 less than Test1
33 eq2 or grater than 4
33 less than 4
33 eq2 or grater than 4
############# (()) use ro number +-*/% ##############
37
29
132
8
1
############# $() use like `` ##################
/usr/bin/pwd
/usr/bin/pwd
################## ${var} is like $var ################
test1
test1
总结:
(1)$(cmd)与··(键盘上1左边的~)一样,都是命令替换,可以将执行结果提取出来
(2)[]使用的时候[ ]前后都必须有空格,且两个字符或数字之间的比较符左右也必须有空格。
(3) []是test的另一种形式,[]中间只能使用= 和 != 比较字符串,如果使用< 、>需要进行转义\。
[]中间如果比较数字需要用 -lt 等符号,不能使用\<比较数字,会当成字符串处理。
(4)[[]]可用于处理逻辑命令,也可以用于处理字符串是否相等,且使用<、>不用转义符.
(5)(())可用于比较数字,且不用转义,而且也可以用于数字计算,比较的时候也是用普通的>,<。(())计算的时候运算符与数字之间不能有空格,例如: sum=$(($sum+4))
(6)字符串比较 用[],与普通的<,>,=,!=符号,如果使用<,>需要转义;或者使用[[]]比较字符串也是用普通符号不用转义
数字比较用[]的时候用-lt,-gt等符号,不能使用\<(因为会当成字符串处理);或者用(())比较数字用普通符号不用转义
(7)可以将$理解为取变量的符号,$var 或者 ${} ,在不影响语义的情况下可以省去{},但是最好写上{}。例如:test=XXX.$testWWWW.这时候就必须加上{}变为${test}WWWW
更多的特殊符号参考:http://www.jb51.net/article/69966.htm
【shell】shell中各种括号的作用()、(())、[]、[[]]、{}
原文:https://www.cnblogs.com/cangqinglang/p/11778988.html