Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know I can source FILE or . FILE a file in Bash, but for some reason I cannot get it to load in the file. I keep seeing "No such file or directory" errors in the console.

Here's the directory structure

/bin/script.sh (file including the config)
/config.sh

I may re-structure this anyways, but for now, I think it is what it is. I'm attempting to include the config file like this:

source ../config.sh
source config.sh

Neither one of those actually works. Any ideas?

share|improve this question
    
Maybe there's a better way to get the path, relative to the script being executed? –  Will Ashworth Mar 6 at 17:38
    
You're in the wrong directory? –  Michael Hampton Mar 6 at 17:46
    
Well that's kind of the issue. Theoretically the script.sh can be executed from just about anywhere on the system. It doesn't really matter where you execute script.sh from...as long as you can supply a correct path to the script to execute it. That's my thinking anyways. –  Will Ashworth Mar 6 at 17:49
3  
This works... if ../config.sh/config.sh is relative to $PWD. You might want to look at this for a lead in the right direction. –  Aaron Copley Mar 6 at 17:51
1  
That totally helped out. Thank you Aaron! –  Will Ashworth Mar 6 at 18:11

1 Answer 1

Aaron's response totally helped out. This is the solution that ended up working for me.

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $SCRIPTDIR/../config.sh;
share|improve this answer
    
Glad to help. I really didn't have time to answer it myself. –  Aaron Copley Mar 6 at 18:20
    
Yeah, getting the path from within the script was an all-around win. That worked beautifully! –  Will Ashworth Mar 6 at 21:18
    
Please see BashFAQ/028. –  Dennis Williamson Mar 7 at 2:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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