Sourcing Scripts

Some scripts were meant to be sourced so that the variables defined by the script live beyond the execution of the script. The following code snippet will help remind users that it must be sourced!

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

display_usage() {
echo "Usage : . myscript.sh <parameter>"
}

# check script is sourced
if [[ $0 != "-bash" ]]; then
echo "Script must be sourced!"
echo ""
display_usage
exit 1
fi