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/enviroments I can't simply use a hardcoded 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 Jul 18 '15 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. – the_velour_fog Jul 18 '15 at 12:13
  • have a look at stackoverflow.com/questions/59895 – michas Jul 18 '15 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 – the_velour_fog Jul 18 '15 at 12:19
  • May be you are looking for [pushd and popd] (softpanorama.org/Utilities/pushd_popd_and_dirs.shtml) – SHW Jul 18 '15 at 12:28

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

| improve this answer | |
  • 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 – the_velour_fog Jul 18 '15 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 Jul 18 '15 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! – the_velour_fog Jul 18 '15 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 Jul 20 '15 at 5:39
echo "$( echo $(cd ../ && pwd) )/includes/setup.sh"


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

echo $ParDir
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.