How can I determine the name of the Bash script file inside the script itself?
Like if my script is in file runme.sh
, then how would I make it to display "You are running runme.sh" message without hardcoding that?
How can I determine the name of the Bash script file inside the script itself?
Like if my script is in file runme.sh
, then how would I make it to display "You are running runme.sh" message without hardcoding that?
Since some comments asked about the filename without extension, here's an example how to accomplish that:
Enjoy!
This resolves symbolic links (realpath does that), handles spaces (double quotes do this), and will find the current script name even when sourced (. ./myscript) or called by other scripts ($BASH_SOURCE handles that). After all that, it is good to save this in a environment variable for re-use or for easy copy elsewhere (this=)...
Here is what I came up with, inspired by Dimitre Radoulov's answer (which I upvoted, by the way).
regardless of the way you call your script
or
if your invoke shell script like
$0 is full name
basename $0 will get the base file name
and you need to put this basic name into a variable like
and add your additional text
so your scripts like