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?
somthing like this?
Info thanks to Bill Hernandez. I added some preferences I'm adopting.
Cheers
Re: Tanktalus's (accepted) answer above, a slightly cleaner way is to use:
If your script has been sourced from another bash script, you can use:
I agree that it would be confusing to dereference symlinks if your objective is to provide feedback to the user, but there are occasions when you do need to get the canonical name to a script or other file, and this is the best way, imo.
You can use $0 to determine your script name (with full path) - to get the script name only you can trim that variable with
For reading through a symlink, which is usually not what you want (you usually don't want to confuse the user this way), try:
IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!? Must be a bug!" Besides, one of the purposes of having differently-named symlinks is to provide different functionality based on the name it's called as (think gzip and gunzip on some platforms).
echo "You are running $0"