Wednesday, June 5, 2013

Remove string postfix in shell

Can't believe I missed this tip for years (and was using all kinds of ugly workarounds).

To quickly remove a postfix of a string in shell:

test=file.ext; echo ${test%.ext}

On the other hand, if ext is not in test, the original string is returned.

PS: There are some more standard ways to deal with extensions, like

FN=$(basename $1 .tex)

But to deal with a string is more general and has more widely usage.