Operating System - Linux
1753445 Members
5207 Online
108794 Solutions
New Discussion

Re: Basename $0 in function

 
SOLVED
Go to solution
Steve Givens
Occasional Advisor

Re: Basename $0 in function

What are the pattern matches described above "saying"?

_ProgName=${0##*/}
_ProgName=${_ProgName%.*}
Bill Hassell
Honored Contributor

Re: Basename $0 in function

The constructs:

> _ProgName=${0##*/}
> _ProgName=${_ProgName%.*}

will return $0 (the script or function name depending on the shell you are using), strip all leading directories (ie, /usr/local/bin/myscript.sh becomes myscript.sh) and the second line takes myscript.sh and strips away any trailing .stuff such as .sh or .exe or whatever. Note that the last construct strips just the last .stuff, so a script named:

myscript.ver3.4.2a

will be truncated to myscript.ver.3.4

Note that Unix has no inherent concept of a type-extension so some script writers will use .ksh or .sh but others skip the extra characters.


Bill Hassell, sysadmin