Sergei Rodionov
Sergei Rodionov

Reputation: 4559

Script directory path regardless of current/working directory

Is there a way to get the script directory regardless of change in current directory that occurred during script execution.

echo $(dirname "$(readlink -f "$0")")
cd /tmp
echo $(dirname "$(readlink -f "$0")")
/home/user/test
/tmp

In the example above, I need /home/user/test both times, without storing it to a variable.

Upvotes: 1

Views: 93

Answers (1)

Cyrus
Cyrus

Reputation: 88999

This should do the job in your script:

dir="$(readlink /proc/$PPID/cwd)"

Upvotes: 1

Related Questions