欢迎来到李先生的博客

深山的鹿,不知归处;万般皆苦,只可自渡。
扩大
缩小

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%%/*} :
 
 
#:     表示去掉左边
%:    表示去掉右边
一个为最小匹配(#、%)
两个为最大匹配(##、%%)
 
可以根据键盘图来区分去左还是去右
 
 

posted on 2017-11-13 15:46  Captain_Li  阅读(1111)  评论(0编辑  收藏  举报

导航