开发者

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

开发者 https://www.devze.com 2022-12-30 00:28 出处:网络
This question already has answers here: 开发者_JAVA技巧 How do I know the script file name in a Bash script?
This question already has answers here: 开发者_JAVA技巧 How do I know the script file name in a Bash script? (25 answers) Closed 5 years ago.

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


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)"


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.


basename $0 will give you the script name

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号