shell脚本中%和%%有什么区别?(What is the difference between % and %% in shell scripting?)

编程入门 行业动态 更新时间:2024-10-11 19:15:17
shell脚本中%和%%有什么区别?(What is the difference between % and %% in shell scripting?)

在bash脚本中,当t="hello.txt"

${t%%.txt}和${t%.txt}返回"hello"

同样适用于${t##*.}和${t#*.}返回"txt" 。

它们之间有区别吗? 他们是如何工作的?

In bash scripting, when t="hello.txt" both

${t%%.txt} and ${t%.txt} returns "hello"

the same goes for ${t##*.} and ${t#*.} returns "txt".

Is there a difference between them? How do they work?

最满意答案

简而言之, %%尽可能少地删除% 。

# t="hello.world.txt" # echo ${t%.*} hello.world # echo ${t%%.*} hello

从bash手册:

'${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.

In short, %% removes as much as possible, % as little as possible.

# t="hello.world.txt" # echo ${t%.*} hello.world # echo ${t%%.*} hello

From the bash manual:

'${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.

                    
                     
          

更多推荐

本文发布于:2023-08-03 15:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   脚本   shell   difference   scripting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!