Showing posts with label substring. Show all posts
Showing posts with label substring. Show all posts

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