tags:

views:

81

answers:

3

I launch script.sh and inside it i want to know what is his name

Is there a standard procedure to know the scripts name ? the idea is to be able to extract the name from teh full path + name contained in $0

Thanks

+1  A: 

basename $0 will give you the script name

Vincent Robert
`basename` doesn't strip extensions unless you ask it to.
Jefromi
fixed, thanks -
Vincent Robert
+2  A: 

Yes, $0 will always contain the name of the script. Use basename to extract the name.

basename /some/really/long/dir/path/myscripts/coolscript.sh will print coolscript.sh

So in your script, you could do this:

my_scriptname="$(basename $0)"
seamus
+2  A: 
script="${0##*/}"

Edit:

This does the same thing as basename $0. It strips off the last slash and everything before it from $0 using Bash's brace expansion.

Dennis Williamson
@Dennis Williamson, would you mind explaining this :) Thanks.
Anders