Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Last active June 7, 2023 12:07
Show Gist options
  • Save hnakamur/542d7e49ffb8e0322d7cd4492ee0f7e3 to your computer and use it in GitHub Desktop.
Save hnakamur/542d7e49ffb8e0322d7cd4492ee0f7e3 to your computer and use it in GitHub Desktop.
Find out whether a bash script is executed or sourced.
#!/bin/bash
if [ $BASH_ARGV0 = -bash ]; then
echo sourced
else
echo executed
fi
@hnakamur
Copy link
Author

hnakamur commented Jun 7, 2023

$ source source_or_run.sh
sourced
$ ./source_or_run.sh
executed
$ bash source_or_run.sh
executed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment