Search code examples
bashscriptingraspbian

basename $0 results in '-bash'


It seems a Bash script on Raspbian I'm using is unable to retrieve it's own name with basename when I'm running it from /etc/profile.d

I always used this to get the name of my script:

ScriptName="$($(which basename) -- ${0})"

It works fine when executing manually. When I capture $0 the results is '-bash' What could be happening here. What would be a better way to get the scriptname?

I tried with

FullScriptName="${BASH_SOURCE[0]}"

and then

ScriptName = "$( basename $FullScriptName)"

but somehow I'm getting:

ScriptName: command not found

Solution

  • Assign and Quote Your Variables Properly

    Fix your syntax and your quoting:

    ScriptName=$(basename "$0")
    

    See Also