shell脚本使用## or %%

今天写脚本的时候,遇到一个文件路径需要去掉右边一部分,当时就想到了这个,但是很久没用过了,很多不记得了,记录一下这种用法
 
1:vim test.sh
#!/bin/bash

location=/file1/file2/file3/file4/a.txt
echo "${location#*/}  :"   ${location#*/}
echo "${location##*/} :"   ${location##*/}
echo "${location%/*}  :"   ${location%/*}
echo "${location%%/*} :"   ${location%%/*}
执行得到结果:
[root@lemon ~]# sh test.sh 
${location#*/}  : file1/file2/file3/file4/a.txt
${location##*/} : a.txt
${location%/*}  : /file1/file2/file3/file4
${location%%/*} :
 
 
#:     表示去掉左边
%:    表示去掉右边
一个为最小匹配(#、%)
两个为最大匹配(##、%%)
 
可以根据键盘图来区分去左还是去右
 
 
原文地址:https://www.cnblogs.com/lemon-le/p/7826648.html