How can a running application in Linux/*nix determine its own absolute path?

Posted by Dave Wade-Stein on Stack Overflow See other posts from Stack Overflow or by Dave Wade-Stein
Published on 2010-05-08T16:02:39Z Indexed on 2010/05/08 16:08 UTC
Read the original article Hit count: 198

Filed under:
|
|

Suppose you run the application 'app' by typing 'app', rather than its absolute path. Due to your $PATH variable, what actually runs is /foo/bar/app. From inside app I'd like to determine /foo/bar/app. argv[0] is just 'app', so that doesn't help.

I know in Linux I can get do

pid = getpid();

and then look at the /proc/pid/exe softlink, but that doesn't work on other *nix. Is there a more portable way to determine the dir in which the app lives?

© Stack Overflow or respective owner

Related posts about linux

Related posts about application