关于脚本里的 ${parameter##world}、${parameter#world}、${parameter%world}、${parameter%%world} (变量替换)

注:本人的学习记录,仅供参考

看脚本看到这样两处代码: 不清楚其中含义

FILE_BASE_PATH=${0%/*}

ARGS_MAKE_FDSC=${1##*--rfile=}

查阅网上资料,这里做个总结记录。

FILE_BASE_PATH=${0%/*}   :

        匹配脚本文件路径 拿掉最后一个'/'后的字符串 ,剩余赋值给变量FILE_BASE_PATH

       即:获取当前脚本文件的路径赋值给 变量  FILE_BASE_PATH

ARGS_MAKE_FDSC=${1##*--rfile=} :

      匹配参数1 中 "--file=" 后的字符串传递给变量ARGS_MAKE_FDSC

$$Shell本身的PID(ProcessID)
$!Shell最后运行的后台Process的PID
$?最后运行的命令的结束代码(返回值)
$-使用Set命令设定的Flag一览
$*所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#添加到Shell的参数个数
$n
$0 :Shell本身的文件名
$1... $n:添加到Shell的各参数值。$1是第1参数、$2是第2参数…。

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see section 3.5.8 Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the `#' case) or the longest matching pattern (the `##' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the `%' case) or the longest matching pattern (the `%%' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

a#*b: 匹配从参数a,首位置 开始到第一个字符b的字符串,删除

a##*b:匹配从参数a,首位置 开始到最后一个字符b的字符串,删除

a%b*: 匹配从参数a,末位置 开始匹配到第一个字符b的字符串删除

a%%b*:匹配从参数a,末位置 开始匹配到最后一个字符b的字符串,删除

注(*象征任意长度的字符串) ,b 也可以是一组随机字符随机长度的字符串

测试:

 

总结:

此功能 用于 脚本里 取路径、文件名、后缀

先赋值一个变量为一个路径,如下:

1

file=/dir1/dir2/dir3/my.file.txt

 

  参考:关于${0##*/} 和${0%/*}_bigcakewshwl的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值