How best to include other scripts?
Solution 1:
I tend to make my scripts all be relative to one another. That way I can use dirname:
#!/bin/sh
my_dir="$(dirname "$0")"
"$my_dir/other_script.sh"
Solution 2:
I know I am late to the party, but this should work no matter how you start the script and uses builtins exclusively:
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/incl.sh"
. "$DIR/main.sh"
.
(dot) command is an alias to source
, $PWD
is the Path for the Working Directory, BASH_SOURCE
is an array variable whose members are the source filenames, ${string%substring}
strips shortest match of $substring from back of $string
Solution 3:
An alternative to:
scriptPath=$(dirname $0)
is:
scriptPath=${0%/*}
.. the advantage being not having the dependence on dirname, which is not a built-in command (and not always available in emulators)