Not sure if it's the best option (will not work if the script has the same name as the shell), but you can check the first parameter ($0
). Example:
$ cat test.sh
#!/bin/ash
echo "Value: $0"
$ ./test.sh
Value: ./test.sh
$ source test.sh
Value: ash
If you want to check if the file was sourced, you can use something like this:
#!/bin/ash
case $0 in
ash) echo "Sourced" ;;
*) echo "Not sourced" ;;
esac