Aaron H. Aaron H. - 3 months ago 7x
Linux Question

Bash: How _best_ to include other scripts?

The way you would normally include a script is with "source"




source incl.sh

echo "The main script"


echo "The included script"

The output of executing "./main.sh" is:

The included script
The main script

... Now, if you attempt to execute that shell script from another location, it can't find the include unless it's in your path.

What's a good way to ensure that your script can find the include script, especially if for instance, the script needs to be portable?


I tend to make my scripts all be relative to one another. That way I can use dirname:


my_dir="$(dirname "$0")"