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:

../adir/script1.sh

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:

...
./script2.sh
...

How can I fix it? Thanks in advance.

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

../bdir/script2.sh

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:

PATH=$PATH:/my_script1dir:/my_script2dir

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

This article has been dead for over six months. Start a new discussion instead.