quickest (i blv) way to get the current program name minus the path (BASH)

Useful in shell scripts when you're trying to get the shell script name without the full path - and easier than awking or cutting. Bash pattern matching and variable manip is fun.
Sample Output
If called from within a shell script named /home/jimmie/foo.sh:

echo $path_stripped_programname


By: jimmie
2009-12-01 06:27:58

What Others Think

I've always used "$(basename $0)", seems to be easier to remember for me :)
rondemara · 557 weeks and 6 days ago
"basename" is an external program while the above uses a shell built-in and is considerably faster: f=$HOME/readme.txt ; echo -n "$(basename $f)\t" ; time (for i in $(seq 1 100000) ; do echo $(basename $f) > /dev/null ; done) readme.txt 2m16.21s real 0m29.38s user 1m35.14s system f=$HOME/readme.txt ; echo -n "${f##*/}\t" ; time (for i in $(seq 1 100000) ; do echo "${f##*/}\t" > /dev/null ; done) readme.txt 0m2.39s real 0m1.42s user 0m0.80s system
penpen · 557 weeks and 6 days ago
Nice. I tend to use basename, but only because I can never figure out the syntax of "${0##*/}" in a hurry!
flatcap · 557 weeks and 6 days ago
I don't anticipate calling basename 100000 times any time soon, so I'm going to stick with it to aid in readability. This appeals but I know I'll forget it. Still a thumbs up though. Thanks.
jgc · 557 weeks and 6 days ago
By defining basename as a function, you can have the best of both worlds: the shell's speed and the ease of use of a command: http://cfajohnson.com/shell/scripts/basename-sh
cfajohnson · 557 weeks and 6 days ago
cfajohnson for the win. Regarding penpen's timings, the slowness of basename is not the string processing, but the spawning of a new process just to do very little work 100000 times.
bwoodacre · 557 weeks and 5 days ago
I'm sorry jimmie, you've exceeded the message length limit for Twitter by more than double. You can start by conforming to accepted usage standards and make use of the proper form "ur" and eliminating all articles and most conjunctions. Or you could spell out "believe" and "manipulation" and avoid inviting sarcasm.
dennisw · 557 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: