In a bash script, I am trying to source another bash script that lives in another directory using $(dirname "$BASH_SOURCE"). Where $(dirname "$BASH_SOURCE") is currently /Users/dank/code/<PROJECT>/utils/jwt.
.
|__ utils
|__ jwt
| |__ rand.sh
| |__ get_jwt.sh # this sources id/create_new_user.sh
|
|__ id
|__ create_new_user.sh
From the project root . (cd /Users/dank/code/<PROJECT>), I am running ./utils/jwt/get_jwt.sh (equivalent to /Users/dank/code/<PROJECT>/utils/jwt/get_jwt.sh). This bash script sources and calls a function inside ./utils/id/create_new_user.sh (equivalent to /Users/dank/code/<PROJECT>/utils/jwt/../id/get_jwt.sh) since $(dirname "$BASH_SOURCE") is /Users/dank/code/<PROJECT>/utils/jwt.
But I am seeing a weird behavior where if I do not source rand.sh (from the same directory), I cannot source id/create_new_user.sh (from another directory):
source $(dirname "$BASH_SOURCE")/rand.sh # Why do I have to have this LOC?
source $(dirname "$BASH_SOURCE")/../id/create_new_user.sh # Does not work without above LOC
I get an error if I delete source $(dirname "$BASH_SOURCE")/rand.sh:
./utils/jwt/../id/create_new_user.sh: line 9: ./utils/jwt/create_new_user.sh: No such file or directory
Why do I have to source rand.sh? Why can't I just source only the script I need?
iddirectory is underjwtyou should remove the../part of the pathsource $(dirname "$BASH_SOURCE")/../id/create_new_user.shtry instead:source $(dirname "$BASH_SOURCE")/id/create_new_user.sh