How do I get the script name being executed in bash?
Try:
readlink -f ${BASH_SOURCE[0]}
or just:
${BASH_SOURCE[0]}
.
Remarks:
$0
only works when user executes "./script.sh"
$BASH_ARGV
only works when user executes ". script.sh" or "source script.sh"
${BASH_SOURCE[0]}
works on both cases.
readlink -f
is useful when symbolic link is used.
create .sh file lets say view.sh then put
#!/bin/bashecho "The script is being executed..."readlink -f ${BASH_SOURCE[0]}