Search code examples
bashcsh

$0 doesn't work when I source a bash script


I have a simple script test.sh

#!/bin/bash
echo $0

When I run the following from csh terminal:

bash -c 'test.sh'

Then the output is test.sh

But when I run:

bash -c 'source test.sh'

The output is bash

Does anybody know how to print the script name in this case?


Solution

  • #!/bin/bash
    declare -r SCRIPT_NAME=$(readlink -f ${BASH_SOURCE[0]})