Having some trouble getting $PWD to work inside a bash script...

I have two scripts in the same directory:

~/outer.sh, ~/inner.sh. I use outer.sh to call inner.sh as follows:

(outer.sh contents shown below)



But this seems not to work. Further investigation shows that $PWD appears inaccessible as I've used it here (nothing appears with printf $PWD >> logfile.txt), and I suspect it has something to do with calling a script from a script... can anyone clarify what's going on here?


$PWD is the current directory, not the directory containing the script. There's no reason why inner.sh would be located in the current directory.

The path to the script is stored in $0. You can extract its directory part to find the directory containing the script.

script_directory=$(dirname -- "$0")

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.