How to Find Script file name in a Bash Script
Abstract: Your Script Name is test.shSNAME=$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")
Command SNAME=$(basename 「$(test -L 「$0」 && readlink 「$0」 || echo 「$0″)」)
While running a bash script using Linux terminal the script name can also passed as argument at $0. but if we are using full path of script to execute, it will show full path as results when print value of $0. So use above command will exactly provide only script name. It also handle if you are running any bash script using symbolic link.
Example:Create a script named test.sh and add following content in script.
#!/bin/bash SNAME=$(basename "$(test -L "$0" && readlink "$0" || echo "$0")") echo "Your Script Name is $SNAME"
Lets execute this script and check for results.
# sh test.sh
Result will be as follows:
Your Script Name is test.sh