In one of my bash scripts I needed to obtain the last part of a colon delimited string. For example I needed to grab the numeric 289283 value from the following value:
OK: DriveC-ReadBytesPerSec: 289283 After some trial and error I arrived at the following:
READRESULT="OK: DriveC-ReadBytesPerSec: 289283" echo ${READRESULT#*:*:*} Which outputs 289283.
The problem is that whilst it gets the job done, I don't fully understand why ${READRESULT#*:*:*} produces the correct result.
Can anyone explain how this globbing expression works?
${READRESULT##*: }, to strip everything until the last colon followed by a space. Note that${READRESULT#*:*:*}has a leading space, which could cause trouble down the line; and as peth writes the last*isn't matching anything.${READRESULT#*:*: }is probably what you meant to write.