What does “variable=${2%?}$i” mean in bash scripting? [duplicate]

萝らか妹 提交于 2019-12-25 01:46:04

问题


Can anyone explain the following bash snippet?

for i in $(seq 1 1 10)
do
   VAR=${2%?}$i
   break;
done

回答1:


It removes the trailing character from $2 (second positional parameter) and concatenates that value with $i

example:

$ v1="myvalue1x"
$ v2="myvalue2"
$ combined="${v1%?}$v2"
$ echo $combined
myvalue1myvalue2

For more info how the substitution works you can check the Parameter Expansion section of the bash manual




回答2:


See the bash man page, section parameter expansion:

   ${parameter%word}
   ${parameter%%word}
          Remove  matching suffix pattern.  The word is expanded to produce a
          pattern just as in pathname expansion.  If the  pattern  matches  a
          trailing  portion  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.

Since ? matches a single character, the trailing character is removed from the second argument of the script.



来源:https://stackoverflow.com/questions/49300452/what-does-variable-2i-mean-in-bash-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!