Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$0 Tweak Advice Request 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

From within a Bourne shell script called MyScript.sh, $0 returns the name for the currently executing script as: ./MyScript.sh

How can I just return the base name without the excess characters "./" and ".sh"?

I just want to return: MyScript


Thanks,

Michael42
 
Have you tried something like this ?
basename $0 .sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

Thanks for the reply. My actual goal is to extract the name without the characters so as I can use it in many scripts for various .tmp, .out and .lst files created.

In most cases I will remove the files but in other cases I need the files to remain and being able to refer to the name as such standardizes all my scripts and meets the business requiremnts for the environment. :)

Any advice?

Thanks,

Michael42
 
Something like this ?
myScriptName=`basename $0 .sh`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

Is basename a UNIX Bourne shell common value (like hostname)?



Thanks,

Michael42
 
basename and dirname are standard unix commands, like hostname.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If you are using a ksh compatible shell like bash, you can use something like this:

Code:
charlie(~)$ zero=/now/is/the/time.sh
charlie(~)$ basename=${zero##*/}
charlie(~)$ echo $basename
time.sh
charlie(~)$ script=${basename%.*}
charlie(~)$ echo $script
time
 
PHV,

>> myScriptName=`basename $0 .sh`

PHV I get it! It took me two days to grok this.

Nicely done sir.

Thanks,

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top