Shell in four models match

POSIX is a shell pattern matching to provide four alternate configuration parameters (the old version of the shell may not support), each structure has two parameters: the variable name (or variable number) and mode.
The first mode:
${variable%pattern}when this mode, shell looks in variable, the end of the pattern to see if it is a given pattern, and if so, from the command line to remove the contents of the variable in the right side of the shortest matching pattern
The second mode:
${variable%%pattern}when this mode, shell looks in variable, the end of the pattern to see if it is a given pattern, and if so, from the command line to remove the contents of the variable in the right side of the longest match mode.
the third mode:
${variable#pattern}this mode when, shell variable in the lookup to see if it is a pattern began to model, and if so, from the command line to remove the contents of the variable in the left side of the shortest matching pattern
fourth mode:
${variable##pattern}when this mode, shell variable in the look, look at the end of pattern pattern whether it is a given, and if so, from the command line to remove the contents of the variable in the right side of the longest match mode
four modes will not change the value of the variable in which only the pattern in * when using a matching symbols, and %%%, and # ## have differences.



[root@root shell]# var=testcase
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var%s*e} 从最右边删除最短匹配
testca
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var%%s*e} 从最右边删除最长匹配
te
[root@root shell]# echo $var  变量没有改变
testcase
[root@root shell]# echo ${var#?e} 从最左边删除最短匹配
stcase
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var#*e}  从最左边删除最短匹配
stcase
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var##*e} 从最左边删除最长匹配,即删除所有

[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var##*s} 从最左边删除最长匹配
e
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var#test} 删除test
case
[root@root shell]# echo $var
testcase
[root@root shell]# echo ${var#tests} 没有匹配
testcase
[root@root shell]#
另外
var=97%
var=${var%s?}
echo $var #打印97

Transfer: https://www.cnblogs.com/oxspirt/p/7242837.html

Published 46 original articles · won praise 13 · views 60000 +

Guess you like

Origin blog.csdn.net/luliuliu1234/article/details/80994385