How do I get the script name being executed in bash? How do I get the script name being executed in bash? shell shell

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.


The variable BASH_ARGV should work, it appears the script is being sourced

$BASH_ARGV


create .sh file lets say view.sh then put

#!/bin/bashecho "The script is being executed..."readlink -f ${BASH_SOURCE[0]}