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?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

2 Answers2

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 )"
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
0

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

SCRIPTHOME="$( cd ../"$(dirname "$0")" ; pwd -P )"
tmace
  • 117
  • 1
  • 8