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 usedlike this:---echo "Usage: ${0##*/}" ....Then it prints the name of the script. Please help me understanding how it works and why it works.

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: http://bashscripts.org/forum/viewtopic.php?f=15&t=491) 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).

Who is online

Users browsing this forum: No registered users and 5 guests

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