LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get path of sourced bashrc file (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-path-of-sourced-bashrc-file-862094/)

Up_head 02-11-2011 01:34 PM

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

David the H. 02-11-2011 02:21 PM

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"


devUnix 02-11-2011 02:22 PM

Quote:

Originally Posted by Up_head (Post 4255387)
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.

ChrisAbela 02-11-2011 04:32 PM

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$

John VV 02-12-2011 12:01 AM

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

ChrisAbela 02-12-2011 02:30 PM

An update on my sub.bashrc script:

Quote:

export MyOwnLocation=$( dirname $( history 1 | awk '{print $3}' ) )
[ $MyOwnLocation = "." ] && export MyOwnLocation=$PWD

Up_head 02-16-2011 05:29 AM

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


All times are GMT -5. The time now is 03:39 PM.