Wes Miller
Wes Miller

Reputation: 2241

How can I tell a Linux Script (ash, not bash) is running "sourced"?

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.

Upvotes: 1

Views: 2211

Answers (1)

Salem
Salem

Reputation: 12986

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

Upvotes: 2

Related Questions