I have a collection of bash scripts and I want to put some common shell options and variable declarations into a "setup.sh" script which would get sourced at the beginning of each script.
My directory structure is like:

├── includes
│   └── setup.sh
└── server_config
    ├── build_server_core.sh
    ├── install_fail2ban.sh

Because the scripts may be run from different computers or environments, I can't simply use a hard-coded path to the setup.sh

Is there a one-line command to source a script in a different directory to the running script?

  • How about source ../includes/setup.sh?
    – michas
    Commented Jul 18, 2015 at 12:04
  • @michas that would work, but would require that all scripts are invoked from the same working directory everytime - that would be ok if it was possible to conditionally check if the script was being invoked from the correct directory - and error + exit if not. Commented Jul 18, 2015 at 12:13
  • have a look at stackoverflow.com/questions/59895
    – michas
    Commented Jul 18, 2015 at 12:15
  • @michas thanks, Ive looked at that, and it does give the path from the script doing the calling, but that path is longer than the parent directory. it would require removal of the last path segment and then concatenating to the sourced path. bit beyond my bash skills unfortunately Commented Jul 18, 2015 at 12:19
  • May be you are looking for [pushd and popd] (softpanorama.org/Utilities/pushd_popd_and_dirs.shtml)
    – SHW
    Commented Jul 18, 2015 at 12:28

3 Answers 3


First get the directory of the script itself and then use relative paths like that:

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$DIR/../includes/setup.sh"

For more info about finding the correct directory have a look at https://stackoverflow.com/questions/59895

  • 1
    thanks but it errors out . bash is just seeing the literal string /home/user/shell-scripts/server_config/build.sh../includes/setup.sh: No such file or directory Commented Jul 18, 2015 at 12:26
  • Are you sure you used the lines exactly as given above? The slash before .. cannot simply disappear and the build.sh part comes out of nowhere... try to run you script with bash -x to see what is going on.
    – michas
    Commented Jul 18, 2015 at 12:32
  • nice one @michas ! I missed out the slash before .. I dont understand though. is that just a bash feature, that you feed it a long path and then just tack on a ../ and it will start traversing backwards up the directories? thats awesome, Ive just never seen that before! Commented Jul 18, 2015 at 12:37
  • It's just the same when you do cd ... On Unix .. is simply a "normal" directory pointing to the parent directory.
    – michas
    Commented Jul 20, 2015 at 5:39

As a one-liner: double nesting dirname also gets the parent. (inspired here)

source $( dirname $( dirname "${BASH_SOURCE[0]}" ) )/_helperlib.sh
echo "$( echo $(cd ../ && pwd) )/includes/setup.sh"


ParDir="$( echo $(cd ../ && pwd) )/includes/setup.sh"

echo $ParDir

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .