Monday, May 12, 2008

Bash Substring Manipulation

I recently completed a project for work to automate pulling specific subversion revisions of code and publishing them to various servers. While relatively simple in practice, a number of operational dependencies had to be considered, and it had to be written in bash. I've done a bit of shell programming in the past, but never really written a "system" in shell. Not surprisingly, I learned a few things in the process, which I'll be sharing on this blog as they come to mind. One thing I learned, which will come in handy down the road, is that bash has a number of builtin substring operations. As an example, I had to trim a number of paths to insure that there wasn't a trailing forward-slash at the end. Obviously, there are a few ways of doing this, but I found a very convenient and simple idiom that I'll be adding to my toolbox.

${string%substring}

removes shortest match of substring from end of string

so, if mydir="/home/travis/"

echo "${mydir%/}" will produce /home/travis
echo "${mydir%/*/}" will produce /home

What's nice as opposed to just chopping the last character off the string is that if the string is already formatted without a trailing slash, nothing will change.

mydir="/home/travis"
echo "${mydir%/}" will produce /home/travis

2 comments:

Unknown said...

fantastic! i'm always using sed which is way less consise for these types of things...
thanks, travis!

Unknown said...

Hi Travis,

This works with ksh too.
Notice that '%' does a substitution from the end of the value and '#' does a substitution form the beginnning of the value. It is useful to get an extension for instance :
[ dvedv326 : oracle : XXX ]> echo ${toto}
/machin/truc.bid
[ dvedv326 : oracle : XXX ]> echo ${toto#*.}
bid

If you have time to lose, try man ksh ;)