Question

How can I tell in an ash script if it is running "sourced" or "normal"? By sourced I mean using the "." or "source" command to launch the script in the current shell.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top