LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-11-2011, 12:34 PM   #1
Up_head
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
How to get path of sourced bashrc file


Hi,

I want to assign the path of a sourced sub.bashrc file to an environment variable. E.g. if I type (or execute) from a known relative location

$ source ../../someDir/sub.bashrc

the sub.bashrc should set a variable like

export MyOwnLocation=/home/user1/unknownlocation/someDir

The problem is I can't use $0 as reference because the script is only sourced not executed.
I also don't want to hardcode the path because the location might change and there will be more copies.

Is there an easy way to create this information from within the the sourced bashrc file?

I use Gnu bash 2.05b on Suse Linux 9.

Many thanks in advance
 
Old 02-11-2011, 01:21 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Are you talking about only if you manually source a file into the shell? I imagine so, because otherwise you'd have that path already encoded into the script that does the sourcing.

I can't think of any way personally to do it from inside the sourced file outside of hardcoding it, since as you've discovered it effectively becomes part of the shell/script when sourced. AFAICT, you'll have to set it manually from the outside.

You can use readlink -f to get the absolute path to it though.
Code:
$ export MyOwnLocation="$(readlink -f ../../someDir/sub.bashrc)"
$ source "$MyOwnLocation"
If the sourced file is always located in a path relative to the location of the original shell/script, as opposed to the pwd, you could use readlink to get the absolute path of that, then use it to build the path to the sourced file. For example:
Code:
shellloc="$(readlink -f $0)"
shellpath="${shellloc%/*}"
source "$shellpath/sub.bashrc"
 
1 members found this post helpful.
Old 02-11-2011, 01:22 PM   #3
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 549

Rep: Reputation: 46
Quote:
Originally Posted by Up_head View Post
Hi,

I want to assign the path of a sourced sub.bashrc file to an environment variable. E.g. if I type (or execute) from a known relative location

$ source ../../someDir/sub.bashrc

the sub.bashrc should set a variable like

export MyOwnLocation=/home/user1/unknownlocation/someDir

The problem is I can't use $0 as reference because the script is only sourced not executed.
I also don't want to hardcode the path because the location might change and there will be more copies.

Is there an easy way to create this information from within the the sourced bashrc file?

I use Gnu bash 2.05b on Suse Linux 9.

Many thanks in advance


First, I am not getting your problem / question.

Secondly, .bashrc file would genrally reside in a user's home directory which can be referenced by:

Code:
source ~/.bashrc
or

Code:
source ~UserName/.bashrc


What does the following mean:

Code:
export MyOwnLocation=/home/user1/unknownlocation/someDir
in your example?

Do you want to export the current directory within which you source .bashrc?

Please, elaborate your exact purpose / problem.
 
Old 02-11-2011, 03:32 PM   #4
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware 14.1
Posts: 467

Rep: Reputation: 87
Is this what you want:

Quote:
chris@darkstar:~$ cat sub.bashrc
history -a
export MyOwnLocation=$( dirname `tail -1 ~/.bash_history | awk '{print $2}'` )
chris@darkstar:~$ cd /tmp
chris@darkstar:/tmp$ source /home/chris/sub.bashrc
chris@darkstar:/tmp$ echo $MyOwnLocation
/home/chris
chris@darkstar:/tmp$
 
Old 02-11-2011, 11:01 PM   #5
John VV
Guru
 
Registered: Aug 2005
Posts: 12,689

Rep: Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687
also a question WHY suse 9 ????
SUSE 9.3 hit "End of life" in April 2007

and why install a dead OS on a desktop

the current version is suse 11.3
 
1 members found this post helpful.
Old 02-12-2011, 01:30 PM   #6
ChrisAbela
Member
 
Registered: Mar 2008
Location: Malta
Distribution: Slackware 14.1
Posts: 467

Rep: Reputation: 87
An update on my sub.bashrc script:

Quote:
export MyOwnLocation=$( dirname $( history 1 | awk '{print $3}' ) )
[ $MyOwnLocation = "." ] && export MyOwnLocation=$PWD
 
1 members found this post helpful.
Old 02-16-2011, 04:29 AM   #7
Up_head
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks David,

The readlink -f option did the trick for me. I source the respective bashrc file (in most cases) via a script, which has a symbolic link to another place. Hence $0 changed depending from where I called the script. But with readlink -f and the known relative path you can resolve that.

So the script calls
Code:
source ../../../sub.bashrc
and the sub.bashrc contains the following code.

Code:
shellloc="$(readlink -f $0)"
shellpath="${shellloc%/*}"
my_loc="$( readlink -f "${shellpath}/../../../" )"
if [ -e ${my_loc}/sub.bashrc ]; then
    export MYOWNLOCATION=$my_loc
fi

ChrisAbela: Your solution works great when I source the bashrc from the command line.


Many thanks for all your answers

Last edited by Up_head; 02-17-2011 at 07:14 AM.
 
  


Reply

Tags
bash


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
set path for different software in the bashrc file kharnab Linux - Newbie 2 08-24-2010 07:03 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM
~/.bashrc not being sourced. hepburnenthorpe Debian 1 04-28-2007 05:52 PM
.bashrc not being sourced shanenin Linux - Software 1 01-05-2005 10:07 PM
.bashrc not sourced koyi Slackware 4 08-02-2003 10:00 AM


All times are GMT -5. The time now is 09:53 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