Your Linux Data Center Experts

In the (distant) past I've used “basename $0” to get the name of the currently running script.

However, several years ago I learned about “${0##*/}”, and have switched over to using that. The benefit is that you don't have to fork a new process, and it's also very concise, but admittedly more archaic than “basename”.

The way I remember it is that you can do “${var%glob}” “${var%%glob}” “${var#glob}” “${var##glob}”, where they strip either at the beginning or the end of the string. The single version is non-greedy match (matches the shortest match) and the double matches greedily. I remember which one is front versus end by thinking that “#” is like a comment, and comments often come at the beginning of the line. :-) So “${0##*/}” means “strip everything before the last / of $0”.

comments powered by Disqus

Join our other satisfied clients. Contact us today.