Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
...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 :-] )
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
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 .
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.