Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a script know his own name in bash? [duplicate]

Tags:

bash

path

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

like image 337
Debugger Avatar asked Dec 09 '22 16:12

Debugger


2 Answers

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)"
like image 83
seamus Avatar answered Dec 12 '22 10:12

seamus


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.

like image 43
Dennis Williamson Avatar answered Dec 12 '22 11:12

Dennis Williamson