A.Stone
A.Stone

Reputation: 137

Source another shell script relative to current script path without $0

I wonder if there's a good way to source another shell script without $(basename "$0") solution, since sometimes "$0" is not set.

For exmaple "$0" would be "-sh" for login shell.

There's same problem with regard to cwd solution.

Upvotes: 0

Views: 411

Answers (1)

codeforester
codeforester

Reputation: 43019

You could use this expression to find out the directory of the parent Bash script:

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Then you can call your script like this:

cd $dir; source ./other_script

or

source $dir/other_script

See this related post: Getting the source directory of a Bash script from within

Upvotes: 3

Related Questions