LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2009, 05:42 PM   #1
Doubi
LQ Newbie
 
Registered: Mar 2009
Location: London, UK
Distribution: Ubuntu 10.04
Posts: 6

Rep: Reputation: 0
Post Can a script know its own location (not cwd)?


Elsewhere people have given a great discussion and solution to how a script can find its own cwd.

The best solution seemed to me to be
Code:
abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
However if you source the script instead of running it, this gives the current working directory from which you called the script, not the directory in which the script itself resides, which makes sense when you think about it.

So, how can a script find out where it actually is?

Here's the scenario: I have a number of slightly different versions of a project in different folders in my home directory, which I want to compile intermittently. Each version contains certain libraries and output folders which have been hard-coded into the build process (not by me) to be located using environment variables. What I want is to be able to have the same script sit in the top level folder of each of these different project versions and source them to set the relevant paths for that version, relative to that top directory, and add them to the environment before I go to build.

So how can the scripts find out where exactly they are, without having to cd into each directory before I source them?



( P.S.
Just to preclude distractions on points of practicality: I liked this quote I found in a related thread:
Quote:
Originally Posted by cfaj View Post
...Furthermore, it is almost never necessary to find the location of the executable. If you think you need it, you are probably trying to solve the wrong problem.
I agree. A better solution would probably be for the build script to optionally take directory flags and work from the env in their absence, but I don't really have the experience to hack up such a thing. It's a big old project (wxWebKit) and I'm quite unfamiliar with it. Perhaps at some point I can make a feature request for a more flexible build process.

In the meantime I think this is an interesting scripting problem anyway :-] )
 
Old 06-29-2009, 08:32 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
The short answer is no.

The long answer is not reliably nor portably. Here’s a hint: if you are willing to live with a dependence on bash, try looking at $BASH_SOURCE.
 
Old 06-29-2009, 09:29 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What am I missing?

No matter what I am doing in a terminal, I can type "pwd" to see where I am. Why cannot a script do the same?
 
Old 06-29-2009, 09:35 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Because he's not asking for his current working directory but for where the script
resides... e.g. if you happened to be in /tmp, and were to source e.g. /etc/bash_profile
he'd like to be able to print /etc from the sourced script, not /tmp


Cheers,
Tink
 
Old 06-29-2009, 10:31 PM   #5
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
What's wrong with `which $0` too easy; I must be missing something too.
 
Old 06-29-2009, 10:35 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Too easy; something must be wrong :)

Quote:
Originally Posted by micxz View Post
What's wrong with `which $0` too easy; I must be missing something too.
Nice one, I like it! We can't *all* be missing something *all* of the time though surely we (I at least) all miss something *some* of the time.
 
Old 06-29-2009, 10:54 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by micxz View Post
What's wrong with `which $0` too easy; I must be missing something too.
His script, like /etc/bash_profile, may not be in the $PATH,
in which case `which $0` won't work, will it ... worse, there
may be a different script with the same name in the $PATH .



Cheers,
Tink
 
Old 06-29-2009, 11:08 PM   #8
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
micxz@phybernuker:/tmp/tester/somewhere> cat where
#!/bin/bash

dirname `which $0`
micxz@phybernuker:/tmp/tester/somewhere> ./where
/tmp/tester/somewhere
micxz@phybernuker:/tmp/tester/somewhere> pwd
/tmp/tester/somewhere
micxz@phybernuker:/tmp/tester/somewhere> echo $PATH | grep tmp

Maybe I'm missing something again' But tmp is not in my path.
 
Old 06-29-2009, 11:13 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
How about . ?
 
Old 06-29-2009, 11:34 PM   #10
gururaj.jois
LQ Newbie
 
Registered: Jun 2008
Posts: 16

Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Because he's not asking for his current working directory but for where the script
resides... e.g. if you happened to be in /tmp, and were to source e.g. /etc/bash_profile
he'd like to be able to print /etc from the sourced script, not /tmp


Cheers,
Tink
This is not possible, bcoz it not (and never,) the current working directory where the executable resides. The current working directory of a process is the path from it was invoked, i.e. process created.

Guru
 
Old 06-30-2009, 12:28 AM   #11
vap16oct1984
Member
 
Registered: Jun 2009
Location: INDIA
Distribution: RHEL-5
Posts: 174
Blog Entries: 3

Rep: Reputation: 38
hello miz,
it will show u always PWD. not the location of script.
 
Old 06-30-2009, 12:52 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,660

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Here is something that should work with bash and ksh.
Code:
...
scriptDir=$(cd $(dirname $0);pwd)
echo $scriptDir
...
 
Old 06-30-2009, 01:04 AM   #13
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Should work but:

source:
micxz@phybernuker:~/scr> cat where
#!/bin/bash
cd /tmp
echo now in: $(pwd)
scriptDir=$(cd $(dirname $0);pwd)
echo $scriptDir

output:
micxz@phybernuker:~/scr> ./where
now in: /tmp
/tmp
 
Old 06-30-2009, 01:09 AM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,660

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Indeed.
The scriptDir setting should be done before the script change its current directory.
 
Old 06-30-2009, 01:15 AM   #15
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Of course' just trying to stir it up' You can always save the var and us it later; Issue seems solved to me? Where's the original poster'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Location of Gnome auto-mount script? aembleton Linux - Newbie 4 05-27-2009 07:00 PM
Getting location of the script which is currently being executed jiju.jacob Linux - Newbie 4 07-21-2008 06:48 AM
Start up script location aking469 Linux - Laptop and Netbook 1 05-22-2006 01:31 PM
Location for script data storage Skazi Linux - General 1 10-15-2005 10:55 AM
Display Manager Script Location cabinetcrafter Mandriva 1 04-06-2005 09:41 PM


All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration