Thursday, February 11, 2016

BASH Script to remove trailing back slash

I was working with a script that was called in an RPM build (in the SPEC file). I passed a directory as a the first variable to the script. The directory ended with a back slash. I needed to remove that backslash since all of my variables started with a backslash.

RPM_BUILD_DIR=""
if [ -n "$1" ]
then
   RPM_BUILD_DIR=${1%/} #REMOVE TRAILING /
fi

So, the bash script checks to see if we have a parameter in the first position and if so remove the trailing backslash.

If the directory string was "/usr/local/bin/" the results are:
"/usr/local/bin"

I searched for examples to do this and they all seemed overly complex. Maybe this one is too simple for some situations, but it works for my needs.

No comments: