how can a shell script determine its own location when being sourced?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
how can a shell script determine its own location when being sourced?
I have a setup.sh script that needs to be sourced, not executed. What can I use inside this script to determine the full path to itself, so I can compute some variables relative to it?
If it was always sourced in the same directory as it resides (not an option) I would of course use "$PWD". If it was being executed, I would use "$0". If it was a python script, I could also use __file__. Etc. But I can't think of a solution for the basic situation here.
Thanks for the help. I never thought of that, and I'm definitely going to use it in some of my scripts.
However, I hesitate to rely on it in production releases. That variable will be the whole command line, including any other commands composed with it using `;', and the very problem I'm having is causing people to do that very often (cd $location; source ./setup.sh; cd $OLDPWD).
Splitting on `;' would be easy enough, but there are lots of other shell metacharacters that could come into play. Though unlikely, the command could be used in conjunction with commands like time and watch, and environment variables are still sitting there unevaluated.
For example,
Code:
time { source "$user_defined_variable"/setup.sh; myprogram; }
Even if I could write the code to determine that path to setup.sh in this case, I would not trust it in general.
Yes, I thought it was a bit kludgy, but it seemed like the file had already been closed by the time the commands started to execute, so in that sense the location of the file really was just 'history'.
I had another look at the man page for bash. Have you looked at the BASH_SOURCE variable? It seems to be giving what you want inside the script.
Thanks a lot! BASH_SOURCE is exactly what I was looking for.
Unfortunately it seems to not be available before bash version 3, and I would like a solution for csh, too, but I can deal with those situations as special cases.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.