Christian
Christian

Reputation: 107

Find file's own path

I'm trying to find what the unix equivalent of the Windows/DOS variable %cd% is. What I'm looking for is an environmental variable or a workaround that will let me set a variable to the path of the file currently running.

For example, if the program is in /home/chris/Desktop but the working directory is /home/chris, what would be the command to get ~/Desktop as opposed to pwd which will give me /home/chris.

Upvotes: 1

Views: 675

Answers (4)

David W.
David W.

Reputation: 107060

In BASH, you can look at the $PWDvariable. That'll show your Present Working Directory. Getting the relationship between the $PWD and where the program is located is a bit trickier. You can look at the $0 variable which should give you the name of the file I ran the following script:

#! /bin/bash
#

echo "PWD = $PWD"
echo "\$0 = $0"

And got the following result:

$ test.sh         
PWD = /Users/david
$0 = /Users/david/bin/test.sh

The $0 gives you the name of the file from the root of the OS. Taking the dirname will give you the file name. Somehow, if you can filter out the PWD from the $0, you might get what you're looking for. I had some luck with the following:

 curPath=$(dirname "${0#$PWD/}")

Didn't thoroughly test it, from what I can see, it seems to do what you want. What it can't do is do something like this:

$ test.sh
PWD = /Users/david/someSubDir
$0 = /Users/david/bin/test.sh
The current path is /Users/david/bin/test.sh

It would have been nice if it could do this:

The current path is ../bin/test.sh

Although the former is correct.

The readlink command doesn't work on non-Linux systems.

Upvotes: 2

Adam Liss
Adam Liss

Reputation: 48310

How about dirname $(readlink -f $0)

readlink -f $0 returns the canonicalized path to the running script.

dirname removes everything after and including the final \.

Upvotes: 2

jpm
jpm

Reputation: 3155

Working with what Chris suggested, you could use the which command. According to the man page, which reports the full path of the executable that would have been executed if its argument had been entered at the shell prompt. Since we know $0 was entered at the shell prompt, we can use `which $0` to report exactly the path that was used to execute. Unfortunately, this still suffers from the symlink issue, as which does not provide options to avoid symlinks.

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 223083

This way works, but isn't 100% reliable:

${0%/*}

The way that works is that it reads $0 (the program name), and strips off everything from the final slash onwards. It's not reliable because if your script is invoked via a symlink, you will get the directory containing the symlink, not the directory containing the real script.

Also, it's possible to pass in a "fake" value for $0, for example by using exec -a. So even if you aren't using symlinks, it's still not a 100% solution. In fact, such a solution doesn't exist.

Upvotes: 0

Related Questions