How do you determine if a file is being sourced from bash?
This is very similar to this question, however, none of the presented solutions work. For example, given the sourced file setup.bash
:
if [ "$_" == "$0" ]
then
echo "Please source this script. Do not execute."
exit 1
fi
If you were to source this with: /bin/bash -c ". ./setup.bash;"
you'll see the message printed "Please source this script. Do not execute.".
Why is this?
I'm sourcing the file this way because my end goal is to execute a bash script that relies on this sourced file, meaning I eventually want to do:
`/bin/bash -c ". ./setup.bash; some_command_that_relies_on_setup_dot_bash;"`
If I run these in bash directly, like:
. ./setup.bash
some_command_that_relies_on_setup_dot_bash
the sourced file doesn't display the message. What am I doing wrong?