How to include one bash script in another

The problem

Let’s say we have two scripts, /foo/bar/utils.sh

and script /foo/bar/main.sh, in which you want to use function do_something_useful from utils.sh.

The common incorrect answer

You can easily bump into following answer on the internet:

Which however doesn’t work unless you your shell’s current working path is the same as path to a directory where the script is located. But – if your working path is /foo, and you run ./bar/main.sh you just receive error message

The correct solution

The solution is simple.

Now even if you cd into different directory, it still works. Sequence of commands

runs with no error and prints message Hello foobar

Leave a Reply

Your email address will not be published. Required fields are marked *