0

Can anyone explain the following bash snippet?

for i in $(seq 1 1 10)
do
   VAR=${2%?}$i
   break;
done
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

2 Answers2

2

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

0

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87