This is a very useful and powerful tool to have in your arsnal. It is power packed. I use this all the time to chop up strings. In essence it is substring
on STEROIDS. I personally think this is proof Brian Fox is a genius.
There are two symbols that you need to remember to use this little trick, the Pound Sign (#) and the Percent Sign (%)
The pound sign (#) matches stuff in the front of the string OR as I like to put it knocks stuff off the front of the string. A good way to remember this is... where does a pound sign go... it's in the front of the string. So the stuff you are going to keep is in the back of the string. More importantly the stuff you are going to knock of is in the front of the string... remember that... it's important. With the pound sign, the stuff you are going to trash is in the front of the string.
The percent sign (%) matches stuff in the back of the string. Or as I like to say... it knocks stuff off the back of the string. Again you can remember this because the percent sign always goes in the back. Therefore, the stuff you are going to keep is in the front of the string. Again and more importantly the stuff you are going to knock off is in the back of the string... that's important too. The stuff you are going to trash is in the back of the string.
Now the format for these two jewels... it starts, like all variable references, with the old dollar sign - or as I like to call it - the String sign. Next is the opening curly brace. All the work is done inside curly braces. Next is the variable itself. So far the construct looks like this:
$ ${myVar...}
There are two parts after the variable and this depends on wheather you are pulling from the front of the string or the back. So lets draw a good old example. We have a string of I like cake.
We want to extract the cake because that's what we all care about anyway. So we are going to use the pound sign because it will trash the stuff in the front of the string. The stuff we don't care about.
$ myVar="I like cake."
One lump or two... how many symbols you use determines how much you keep or knock off. One pound sign matches the minimum then gives up while two matches as much as it can. So we want to take the cake... out of the string. The best place to split is is on the space. Since there are two spaces in our way we want to match the most. In regex they call this being greedy. So we need to use two pound signs so we can match the most in the front of the string (knock it off) to get to our cake.
$ ${myVar##...
Now we need to get the pattern right with some globbing. What we want to match on is the space. So that needs to go in our construct. The second thing we need is to tell it where to match this space at. That's the globbing part... i.e. the astrisk (*). This is the important part to remember as stated earlier. We are knocking stuff off the front of the string with the pound signs. That means our pattern is going to address what we are trashing. So the astrisk goes first, then the space, because that's where the matching stuff is. Remember, we are trashing the stuff in the front of the string so the astrisk goes in the front. That's why it's important... now this is our construct.
$ ${myVar##* }
And this is that construct in action...
$ myVar="I like cake"
$ echo ${myVar##* }
$ cake
Now you know the rest of the story.