5

I've got the following variable set in bash:

ver=$(/usr/lib/virtualbox/VBoxManage -v | tail -1)

then I have the following variable which I do not quite understand:

pkg_ver="${ver%%r*}"

Could anyone elaborate on what this does, and how pkg_ver is related to the original ver value?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Ivan I. Iliev
  • 55
  • 1
  • 6

2 Answers2

10

It is a bash parameter expansion syntax to extract text from end of string upto first occurrence of r

name="Ivory"
printf "%s\n" "${name%%r*}"
Ivo

${PARAMETER%%PATTERN}

This form is to remove the described pattern trying to match it from the end of the string. The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.

Inian
  • 80,270
  • 14
  • 142
  • 161
1

You will get everything from variable ver until first "r" character and it will be stored inside pkg_ver.

export ver=aaarrr
echo "${ver%%r*}"
aaa
Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • 3
    The `export` is entirely unnecessary here. Environment space is a limited resource -- the more content you have exported to the environment (and thus copied into the starting environment for other programs your script starts), the shorter the maximum command-line length when starting new processes can be. – Charles Duffy Jan 25 '17 at 19:17