Bash

Bash sub string stripping

Often when making long strings (like json), there will be trailing comma that needs to be deleted.  The % makes this possible in bash.

# Example One
# note the extra , at the end
MYVAR="cat, dog, bird,"
MYNEWVAR=${MYVAR%,}

echo $MYNEWVAR
# outputs 'cat, dog, bird'



# Example Two
# strip off the file known extension
MYFILE="foo.csv"
MYFILENOEXT=${MYFILE%\.csv}

echo $MYFILENOEXT
# outputs 'foo'

See http://www.tldp.org/LDP/abs/html/string-manipulation.html for more string functions.