0

I am running a shell script on bash in linux. I want to the get absolute path to the parent directory of the folder where my script is located.

Following gives me the path to where my script is located

SCRIPTHOME="$( cd "$(dirname "$0")" ; pwd -P )"

Thanks to the answer in following discussion:
Reliable way for a Bash script to get the full path to itself

Question:
What should I change in SCRIPTHOME="$( cd "$(dirname "$0")" ; pwd -P )" to get the parent directory of the folder where my script is located?

2 Answers 2

3

Just add /.. after the cd. This assumes your script can never be in root, as that will still more or less "work", it just won't give you a parent directory (since there isn't one), it'll just give you /.

SCRIPTHOME="$( cd "$(dirname "$0")"/.. ; pwd -P )"
0
0

I believe you could also add ../ before the command:

SCRIPTHOME="$( cd ../"$(dirname "$0")" ; pwd -P )"

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