The double percentage means match the LONGEST pattern FROM THE END of the variable.
If we tried the previous command using this new construct:
echo ${MAGIC%%r*a} |
we should end up with:
ab |
Why is it leaving the ab? Because it's matching the longest match of an 'r', followed by any number of characters ( * ) followed by an 'a', removing the matched pattern, echo'ing the remainder.
So where is this sort of thing used? Well perhaps you've got a long path:
SCRIPT=/home/hamish/scripts/testing/myscript.sh |
Let's say we want to extract the path but not the script name itself. Then we would type:
THE_PATH=${SCRIPT%/*} |
This construct would mean: from the end of the string, match the shortest pattern of a forward slash followed immediately by any number of characters, resulting in:
echo $THE_PATH |
or
/home/hamish/scripts/testing |