Hi all,
Not sure where to find the answer to this question. Google wasn't helpful. The users on this list are a great repository of knowledge so I thought to try here.
Is there a bash command that tells an executing script what *its* path is? Not the path where the user is but where the script is. If not that then a series of commands that yield the same result? Maybe some way of using 'ps'?
Has me stumped and my dog-eared "UNIX in a Nutshell" hasn't exposed the goodies either ;)
TIA, Mike Wright
On Mon, Jan 31, 2011 at 12:32:24 -0800, Mike Wright mike.wright@mailinator.com wrote:
Hi all,
Not sure where to find the answer to this question. Google wasn't helpful. The users on this list are a great repository of knowledge so I thought to try here.
Is there a bash command that tells an executing script what *its* path is? Not the path where the user is but where the script is. If not that then a series of commands that yield the same result? Maybe some way of using 'ps'?
Has me stumped and my dog-eared "UNIX in a Nutshell" hasn't exposed the goodies either ;)
What is the high level purpose for wanting this information? There may be other ways to solve that problem.
Bruno Wolff III wrote:
On Mon, Jan 31, 2011 at 12:32:24 -0800, Mike Wright mike.wright@mailinator.com wrote:
Hi all,
Not sure where to find the answer to this question. Google wasn't helpful. The users on this list are a great repository of knowledge so I thought to try here.
Is there a bash command that tells an executing script what *its* path is? Not the path where the user is but where the script is. If not that then a series of commands that yield the same result? Maybe some way of using 'ps'?
Has me stumped and my dog-eared "UNIX in a Nutshell" hasn't exposed the goodies either ;)
What is the high level purpose for wanting this information? There may be other ways to solve that problem.
Awesome, Bruno, thanks for responding.
I'm trying to create a self-contained app in that it is both a data repository and also the location of its binary (BIN/runme, DATA/) so that the application could be installed anywhere in user space and still be able to find itself. A link in /usr/local/bin (wherever) would point to the install location.
Ideas?
Mike
On Mon, Jan 31, 2011 at 12:32 PM, Mike Wright mike.wright@mailinator.com wrote:
s there a bash command that tells an executing script what *its* path is? Not the path where the user is but where the script is.
$ cat ./test.sh #!/bin/bash
echo $0 is the script echo `dirname $0` is the script location
exit $?
HTH
On Mon, Jan 31, 2011 at 12:55:36 -0800, Mike Wright mike.wright@mailinator.com wrote:
I'm trying to create a self-contained app in that it is both a data repository and also the location of its binary (BIN/runme, DATA/) so that the application could be installed anywhere in user space and still be able to find itself. A link in /usr/local/bin (wherever) would point to the install location.
That rules out doing some things with the fd directly, since that fd doesn't point to the data.
If you know the program is being run via a sym link, then the readlink based suggestions sound like they will do what you want.
On Mon, 2011-01-31 at 14:49 -0700, compdoc wrote:
Is there a bash command that tells an executing script what *its* path is?
echo $PWD
No, that just tells where it was executed from. The OP wants to know where the script actually lives.
poc
dirname $0
suomi
On 2011-02-01 13:03, Patrick O'Callaghan wrote:
On Mon, 2011-01-31 at 14:49 -0700, compdoc wrote:
Is there a bash command that tells an executing script what *its* path is?
echo $PWD
No, that just tells where it was executed from. The OP wants to know where the script actually lives.
poc
On 02/01/2011 05:23 AM, fedora wrote:
dirname $0
suomi
On 2011-02-01 13:03, Patrick O'Callaghan wrote:
On Mon, 2011-01-31 at 14:49 -0700, compdoc wrote:
Is there a bash command that tells an executing script what *its* path is?
echo $PWD
No, that just tells where it was executed from. The OP wants to know where the script actually lives.
poc
The value of $0 on a Linux system will be the full path to where the script was found, so 'dirname $0' works.
However, bash runs on other OS's (Solaris for one), and Solaris does not provide the full path in $0, you only get the name of the script.
So be careful if you're writing code that needs to be portable.