Linux 中变量处理,如何删除左侧第一个句号后边的所有内容,右侧最后一个句号后边的所有内容
001、删除左侧第一个句号后边的所有内容
[root@PC1 test]# test="kk.aa.ee.ww" ## 测试变量 [root@PC1 test]# echo $test kk.aa.ee.ww [root@PC1 test]# result=${test%%.*} ## 删除左侧第一个句号后边的所有内容 [root@PC1 test]# echo $result kk
。
002、
[root@PC1 test]# test="kk.ee.ww.rrrr.ees" ## 测试变量 [root@PC1 test]# echo $test kk.ee.ww.rrrr.ees [root@PC1 test]# result=${test%.*} ## 删除右侧最后一个句号后边的所有内容 [root@PC1 test]# echo $result kk.ee.ww.rrrr
。
变量截取的四种情况:
[root@PC1 test]# ls [root@PC1 test]# test="aa.bb.cc.dd" [root@PC1 test]# echo ${test%%.*} ##.* 表示向后通配 aa [root@PC1 test]# echo ${test%.*} aa.bb.cc [root@PC1 test]# echo ${test##*.} ## *.表示向前统配 dd [root@PC1 test]# echo ${test#*.} bb.cc.dd
。