0

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?

8
  • 1
    if id directory is under jwt you should remove the ../ part of the path Commented May 19, 2018 at 16:07
  • @alfasin my mistake. let me edit the file structure. Commented May 19, 2018 at 16:08
  • 1
    I was referring to this line source $(dirname "$BASH_SOURCE")/../id/create_new_user.sh try instead: source $(dirname "$BASH_SOURCE")/id/create_new_user.sh Commented May 19, 2018 at 16:10
  • 1
    You have run it from the folder above utils Commented May 19, 2018 at 16:31
  • 1
    Troubleshooting suggestion: make a copy of rand.sh, and try running that instead. If it works, start removing parts of the rand-copy.sh script and see when it stops working -- when it stops working, whatever you just removed was what made it work, and what that is will be a clue to what's going on. Commented May 19, 2018 at 18:42

1 Answer 1

1

I can not see any reason why you should need rand.sh.

I just tried to replicate your situation, using the following code:

utils/jwt/get_jwt.sh

#!/bin/bash

echo 'Get jwt'
source $(dirname "$BASH_SOURCE")/../id/create_new_user.sh
echo 'All done'

utils/id/create_new_user.sh

#!/bin/bash

echo 'create new user'

Running this using ./utils/jwt/get_jwt.sh creates the expected output:

Get jwt
create new user
All done

Sourcing the file in the described manner is valid, your problem must be something else in your script.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting... In which directory did you run the script?
I added this to my answer: like you from the project root using ./utils/jwt/get_jwt.sh

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.