It is currently Thu Mar 30, 2017 12:30 pm

Help undesrtanding what ${0##*/} means

All times are UTC - 6 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jun 01, 2009 8:06 am   

Joined: Mon Jun 01, 2009 7:49 am
Posts: 1
Hi forum

Greetings to all !
I am a newbie in shell programming and need help understanding some basic expressions in a script.
I have seen the expression ${0##*/} in many scripts but do not understand the principle behind it.
I tried echo ${0##*/} in shell and it returns "bash". When i execute the scripts where it is being used
like this:
echo "Usage: ${0##*/}"
Then it prints the name of the script.
Please help me understanding how it works and why it works.

Best Regards

 PostPosted: Sat Jun 13, 2009 9:05 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
What it basically does is returns the name of the program or script you are running. If you just run it in a bash shell, you are running bash, so that's what it will return. The ${ ... } is used to surround a variable, and will return the value of that variable. $0 is a positional parameter (see here: and the 0th parameter is the name of the script that was used to execute it, including the path if one was used; that's what the 0 is for. The ## is to match the largest possible substring that matches the following pattern within the preceding variable's value. In this case, the pattern is */ which will match anything that ends with a '/' because * is a wildcard. So, in short, this removes the path preceding the script name that you are running (if it exists).

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group