How to source file in same dir

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-23

152 How to source a file in the same dir in bash

This won't work

source .env
# - $0 is the path to the current script from where you are calling it. Note that
# it will change as you move up and down subdirectories.
# - dirname gets the containing directory
source "$(dirname -- "$0")/.env"

dirname example:

/Users/jackkinsella/Code/learning/code-diary
dirname $(pwd)0
# i.e. goes one folder up
/Users/jackkinsella/Code/learning