LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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

Reply
 
LinkBack Search this Thread
Old 02-12-2008, 09:09 PM   #1
DeuceNegative
LQ Newbie
 
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29

Rep: Reputation: 16
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 in advance for any help.
 
Old 02-13-2008, 09:18 PM   #2
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
How about putting

cmdline=`history 1`

at the start of the script? Then $cmdline gives the commandline for the source command.
 
Old 02-15-2008, 04:12 PM   #3
DeuceNegative
LQ Newbie
 
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29

Original Poster
Rep: Reputation: 16
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.
 
Old 02-15-2008, 10:59 PM   #4
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
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.
 
Old 02-19-2008, 12:51 PM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
${BASH_SOURCE} should be just what you're asking for.

${BASH_SOURCE%/*} should give you the path you need.

${BASH_SOURCE##*/} would give just the actual name of the file, just in case you need to know the name it was called by.

BTW, what did you do/use?


Refs
In the bash man page, which is long & sometimes confusing, search for:
  • BASH_SOURCE
  • ${parameter#word}
 
Old 02-21-2008, 01:57 PM   #6
DeuceNegative
LQ Newbie
 
Registered: May 2006
Location: Boston, MA
Distribution: Gentoo
Posts: 29

Original Poster
Rep: Reputation: 16
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.

For the vast majority of users, problem solved.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to determine if file size equals zero jonfa Programming 5 12-11-2007 04:48 PM
Check that script is being sourced trek413 Linux - Software 1 11-02-2006 11:38 AM
How can I determine the path of a shell script that I'm running right now ? basher400 Linux - General 7 10-25-2006 08:31 AM
zsh return 0 if script sourced not executed cs-cam Linux - General 2 08-12-2006 11:41 AM
shell script to read ps -e output and determine process double processes. dr_zayus69 Programming 1 09-21-2005 05:37 PM


All times are GMT -5. The time now is 04:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration