Let's do some pattern matching parameter substitutions. Again, we set our MAGIC variable:


The first of these constructs is:


The % symbol matches a pattern from the end of the variable. If we were to run the above construct it will start at the end of our variable MAGIC, searching for the pattern. Thus it will start from the right hand side of the word 'abracadabra'.

What patterns are we meaning?

Well it matches all the pattern syntax that we saw previously. Remember when we discussed wildcards:

* any characters (0 or more)
? any single character
[ ] range of characters
[!] any except those in range

So, let's try and use this:

echo ${MAGIC%a*a}

Now, what the construct matches is the shortest pattern FROM THE END of the variable.

When we match this, an 'a' on the end of the variable, followed by any number of characters ( * ) followed by an 'a': (BUT, the a*a must match the SHORTEST match from the END of the string). Our resulting match:


Once the match is removed, we are left with:


Let's try something a little more adventurous. Match the following:

echo ${MAGIC%r*a}

Again, the shortest match from the end of the string removes the string 'ra' from the end, leaving us with:


Aha, but wait, there"s MORE!