0

I would like to get the absolute path to an executed script. Its path is /use/local/lib/debugger.sh. And it is called through $PATH=/use/local/lib from another folder. I searched an example script and found following one.

echo "path = $(cd $(dirname $0) && pwd)"

I expected I can get the absolute path, /use/local/lib. But the result was not what I expected. I got the absolute path to the directory where the script is called.

How can I get the absolute path where the script exist?

Thank you very much.

0

3 Answers 3

2

Adding the -P option to pwd would make sure symlinks (if any) in the dirname of the script are properly resolved:

path=$(cd $(dirname $0) && pwd -P)
Sign up to request clarification or add additional context in comments.

1 Comment

This is a help, but it doesn't solve what OP wants
1

Use this:

#!/bin/bash

path="path = $(cd $(dirname $0) && pwd)"
script_name="${0##*/}"

echo "${path}/${script_name}"

Or in "oneliner" style echo "path = $(cd $(dirname $0) && pwd)/${0##*/}"

Comments

1

If it is really in your $PATH then the "which" command should find it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.