Linux – How to know the script file name in a Bash script


How can I determine the name of the Bash script file inside the script itself?

Like if my script is in file, then how would I make it to display "You are running" message without hardcoding that?

Best Solution

me=`basename "$0"`

For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try:

me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"

IMO, that'll produce confusing output. "I ran, but it's saying I'm running!? 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).

1 That is, to resolve symlinks such that when the user executes which is actually a symlink to, you wish to use the resolved name rather than