You need to understand how "variable expansion" works: lets assume we have a variable assigned (i suggest you try the examples yourself at the shell prompt and play around a bit with them to get familiar):
var="abc/def/ghi"
The point of shell variables is that you cannot use them directly, like in other programming languages:
x="acd"
y="def"
z=x+y
print z
This (or similar constructs) would work in other languages, but in the shell you use the variable "indirectly", once you have assigned it, through a set of quasi-functions. This is "variable expansion". The most basic expansion is:
${var}
which will expand to the content of the variable "var". This content is replaced at the command line and then the command line is executed. For instance:
# print - "${var}" # your command
# print - "abc/def/ghi" # the shell first expands the expression
abc/def/ghi # excuting the print command
Keep in mind this mechanism when we discuss more complicated expansions. The next in the list are these:
${var#<regex>} ${var##<regex>}
${var%<regex>} ${var%%<regex>}
The first one ("#") takes the content of the variable, then takes the regexp, expands that and if it matches the beginning of the content, the matching part is cut off. Sounds complicated? OK, here is an example with our variable from above:
# print - "${var#?}"
bc/def/ghi
The complete content would be "abc/def/ghi". The regexp ("?") means "any one single character", which is deleted from the beginning, therefore leaving the first character out. Notice, that this DOES NOT CHANGE the variable at all:
# print - "${var#?}"
bc/def/ghi
# print - "${var}"
abc/def/ghi
The opposite of "#" is "%", which works the same, but takes away from the content at the end instead of the beginning. Also notice the "*", which means "any number of any characters". In case you wonder: yes, these are the same characters you can use as filemasks when issuing a "ls -l <mask>". Whatever you can use there you can use here:
# print - "${var%?}"
abc/def/gh
# print - "${var%/*}"
abc/def
You might wonder what the difference between "#" and "##" and "%" and "%%" respectively is. Try out the following and notice the difference:
# print - "${var%/*}"
# print - "${var%%/*}"
# print - "${var#*/}"
# print - "${var##*/}"
The one is always the shortest possible match the other the longest possible match. For matches which only occur once there is no difference.
There are a lot of other interesting and powerful expansions: you can replace one substring with another:
${var/<search>/<replace>}
and a lot of other things. Check out "man ksh" for reference.
I'd like to show you another trick: nested expansions. You can use an expansion inside another expansion, even if it uses the same variable (because - you know already - the variables content itself is not changed!). This, finally, will do what you look for:
Remember what this gives:
${var%?}
Correct: everything save for the last character. Now, let us use this as the regexp we want to take away from the beginning of the content. Obviously this will match most of the content and only leave the last character, yes?
# print - "${var#${var%?}}"
i
And because this will always be true you can use this regexp every time, regardless of what the content of "var" is, for the last characters - or more characters, if you modify it a bit:
# print - "${var#${var%??}}" # the last 2 characters
# print - "${var%${var#??}}" # the first 2 characters
I hope this helps.
bakunin