I have a shell script, /path/adir/script1.sh, that calls another script, /path/bdir/script2.sh. Now, I am in the directory /path/cdir/, and I call the first script like this:


The problem is that script2.sh cannot be found. In fact, even if script2.sh is in the same directory as script1.sh, the same problem occurs. The file script1.sh looks like this:


How can I fix it? Thanks in advance.

Recommended Answers

All 3 Replies

I realized I didn't make myself clear enough: I don't want to call script2.sh in the following way:


because script1.sh could be called from any other directory and the above way won't work. However, it can be assumed that these two scripts are in their relative directories (though their parent directory may differ).

Just add a PATH variable into your script:


That way, you don't have to worry about using things like ./ and ../ -- just call them by their name, and if they're in the search paths that you specified, then you'll be in good shape.

You can get the path of your script with $0 : SCRIPT_DIR=`dirname $0` #relative path or, to get absolute path :

INITIAL_DIR=`pwd` # Save current dir
cd `dirname $0`       # Go to script dir
SCRIPT_DIR=`pwd` # Save script dir
cd ${INITIAL_DIR}   # Go back to initial directory

So, put the few lines above in script1.sh
And call script2 with : ${SCRIPT_DIR}/script2.sh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.