-1

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
  • 5,773
  • 8
  • 45
  • 124

2 Answers2

1

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
  • 20,876
  • 5
  • 42
  • 66
1

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

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