1
#!/bin/bash

cmdname=${0##*/} 
tmplogfile="/tmp/."$cmdname".log"

echo $cmdname
exit 0

I have above shell script to do some task. I am not understanding what ${0##*/} means here. I changed ${0##*/} to ${0}, results are same. Could somebody tell me what the extra ##*/ means?

Thanks.

3
  • You can check Parameter Expansion Commented Mar 20, 2015 at 5:44
  • It is more or less equivalent to cmdname=$(basename "$0"); you can look up the basename command quite easily. Commented Mar 20, 2015 at 6:44
  • @JonathanLeffler After reading the following answer, I also got your point. thanks. Commented Mar 20, 2015 at 7:14

1 Answer 1

2

This is a shell variable substitution notation for removing the largest prefix pattern matching the expresion after ##. This expression is a shell pattern rather than a regular expression one. In this case, its removes the longest prefix from $0 ending in /. For example, for the script below:

/home/user/> cat /home/user/script
echo 'Value of $0      ' : $0
echo 'Value of ${0##*/}' : ${0##*/}

/home/user/> sh script
Value of $0       : script
Value of ${0##*/} : script

/home/user/> sh /home/user/script
Value of $0       : /home/user/script
Value of ${0##*/} : script

From FreeBSD sh manpage:

${parameter##word}

Remove Largest Prefix Pattern.  The word is expanded to produce a
pattern.  The parameter expansion then results in parameter, with
the largest portion of the    prefix matched by the pattern deleted.

Although this is FreeBSD sh manpage, the same applies to all Bourne like shells around.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.