What is the meaning of the ${0##…} syntax with variable, braces and hash character in bash?

前端 未结 3 1336
野性不改
野性不改 2020-11-29 20:04

I just saw some code in bash that I didn\'t quite understand. Being the newbie bash scripter, I\'m not sure what\'s going on.

echo ${0##/*}
echo ${0}
         


        
3条回答
  •  情深已故
    2020-11-29 20:26

    Linux tip: Bash parameters and parameter expansions

    ${PARAMETER##WORD}  Results in removal of the longest matching pattern from the beginning rather than the shortest.
    for example
    [ian@pinguino ~]$ x="a1 b1 c2 d2"
    [ian@pinguino ~]$ echo ${x#*1}
    b1 c2 d2
    [ian@pinguino ~]$ echo ${x##*1}
    c2 d2
    [ian@pinguino ~]$ echo ${x%1*}
    a1 b
    [ian@pinguino ~]$ echo ${x%%1*}
    a
    [ian@pinguino ~]$ echo ${x/1/3}
    a3 b1 c2 d2
    [ian@pinguino ~]$ echo ${x//1/3}
    a3 b3 c2 d2
    [ian@pinguino ~]$ echo ${x//?1/z3}
    z3 z3 c2 d2
    

提交回复
热议问题