在bash脚本中,“variable = $ {2%?} $ i”是什么意思?

时间:2018-03-15 13:15:19

标签: bash shell scripting

任何人都可以解释以下bash片段吗?

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

2 个答案:

答案 0 :(得分:1)

它从$2(第二个位置参数)中删除尾随字符,并将该值与$i

连接起来

示例:

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

有关替换的详细信息,您可以查看bash手册的Parameter Expansion部分

答案 1 :(得分:0)

请参阅bash手册页的参数扩展部分:

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

由于匹配单个字符,因此将从脚本的第二个参数中删除尾随字符。