LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   set current directory as environment variable (https://www.linuxquestions.org/questions/linux-newbie-8/set-current-directory-as-environment-variable-819051/)

tank junior 07-10-2010 04:49 AM

set current directory as environment variable
 
Hi,

How do you set current directory (full path) in an environment variable?
For example your current directory is this:
Code:

/home/<user>/downloads/applications/
What I need is some thing like this:
Code:

export MYHOME=this
doing so $MYHOME should point to '/home/<user>/downloads/applications/'.

Hope it would be simple.

Prashant

druuna 07-10-2010 04:52 AM

Hi,

The pwd command shows the current working directory.

export CURRENTDIR=`pwd`

Hope this helps.

unSpawn 07-10-2010 05:17 AM

Doesn't 'export CURRENTDIR=`pwd`;' only work if you are in that directory? Could include it in the users path (not in /etc/profile, /etc/bashrc or any /etc/profile.d/*) but ~/.bashrc (as it's read on interactive and non-interactive shell start):
Code:

[ -d "${HOME}/downloads/applications" ] && PATH=$PATH:${HOME}/downloads/applications
export PATH


tank junior 07-10-2010 05:25 AM

Thanks drunna,

Now this may be a dumb question but I am new to Linux.
I am using this command in a "run.sh" file.
Code:

export MYHOME=`pwd`
echo $MYHOME

From console, if I run "./run" from the same directory where "run.sh" is it prints the current directory. In case if execute "run.sh" using path, for example:
Code:

/home/user/downloads/applications/./run
It print "home/user", the directory from where you are issuing command. What I need is a way where inside "run.sh", I can access the directory where it resides.

Inside "run.sh", I am setting some vars related to directory structure. For example:
Code:

export PYTHONHOME="lib/"
If I execute "run.sh" using path, it won't work.

Hope I explained well.

Prashant

druuna 07-10-2010 05:29 AM

@unSpawn:
Quote:

Doesn't 'export CURRENTDIR=`pwd`;' only work if you are in that directory?
Yes. I assumed that the example given was just that, an example. That combined with the OP's description of the problem led me to my answer.

But you are correct, it does depends on where it is used (my answer will not work when used in the start-up files mentioned by you).

Let's wait and see what the OP really wants :)

EDIT
Too slow..... OP already replied.
/EDIT

druuna 07-10-2010 05:39 AM

Hi,

If I create a script like this:
Code:

#!/bin/bash

export MYHOME=`pwd`
echo $MYHOME

In my home directory (/home/druuna) and execute it from another directory it works:
Quote:

$ pwd
/home/druuna
[exile] druuna ~ $ cat run.sh
#!/bin/bash

export MYHOME=`pwd`
echo $MYHOME
[exile] druuna ~ $ ./run.sh
/home/druuna
[exile] druuna ~ $ cd /data/Downloads/
[exile] druuna /data/Downloads $ pwd
/data/Downloads
[exile] druuna /data/Downloads $ ~/run.sh
/data/Downloads
[exile] druuna /data/Downloads $ /home/druuna/run.sh
/data/Downloads

If you want to add the lib/ part you need to tell the script where to add it too:
Code:

#!/bin/bash

export MYHOME=`pwd`
echo $MYHOME

export PYTHONHOME="$MYHOME/lib/"
echo $PYTHONHOME

Now it will show the complete path:
Quote:

$ cd _Schuur/Bin/
[exile] druuna ~/_Schuur/Bin $ pwd
/home/druuna/_Schuur/Bin
[exile] druuna ~/_Schuur/Bin $ /home/druuna/run.sh
/home/druuna/_Schuur/Bin
/home/druuna/_Schuur/Bin/lib/
[exile] druuna ~/_Schuur/Bin $ cd /data/Downloads/
[exile] druuna /data/Downloads $ /home/druuna/run.sh
/data/Downloads
/data/Downloads/lib/
Hope this helps.

David the H. 07-10-2010 05:41 AM

There's already a variable that holds the present working directory. Oddly enough, it's "$PWD".

tank junior 07-10-2010 07:38 AM

Hi druuna,

I think you are not getting me. Your script is in "/home/druuna" and when you are executing it from "/data/Downloads",
it's printing "/data/Downloads". It has to print "/home/druuna", the directory where "run.sh" resides.

Here is the script in "/home/<user>/downloads/application/run.sh"

The contents of the script:
Code:

export MYHOME=`pwd`
echo $MYHOME
echo $PWD
echo pwd

My current directory is:
Code:

prashant@ubuntucomp:~$
I am executing "run.sh" from here using full path:
Code:

prashant@ubuntucomp:~$ /home/prashant/downloads/application/./run.sh
It prints:
Code:

/home/prashant
/home/prashant
/home/prashant

The path I need:
Code:

/home/prashant/downloads/application
Cheers

Prashant

druuna 07-10-2010 11:43 AM

Hi,

In your first post you mentioned this: How do you set current directory (full path) in an environment variable?

The current working directory is always seen from where one starts the script (not where the script itself resides).

If your script resides in /home/<user>/downloads/application/ and you run it from /home/prashant, the pwd command will print /home/prashant and if you run it from /tmp it will print /tmp.

I'm not sure if the /home/<user> part needs to be dynamic in your case (unsure as in: are you asking the correct question). If so you can add the extra part (downloads/application) as mentioned in post #6.

If it does not need to be dynamic, but is always /home/prashant/downloads/application you can declare that in one go (export MYHOME=/home/prashant/downloads/application).

Dynamic example:
Quote:

$ ls -l /data/Downloads/run.sh
-rwxr-x--- 1 druuna internet 97 Jul 10 12:37 /data/Downloads/run.sh

$ pwd
/home/druuna

$ cat /data/Downloads/run.sh
#!/bin/bash

export MYHOME=`pwd`
echo $MYHOME

export PYTHONHOME="$MYHOME/lib/"
echo $PYTHONHOME


$ /data/Downloads/run.sh
/home/druuna
/home/druuna/lib/

$ cd /tmp/
$ /data/Downloads/run.sh
/tmp
/tmp/lib/
The bold part adds a directory (lib in the first example) to the output og pwd. You now have 2 exported variables:
MYHOME -> holds the current working directory, which is dynamic and depends on where the script is started (/home/druuna in the first example)
PYTHONHOME -> holds the content of MYHOME + the added directory, you end up with: /home/druuna/lib

Hope this clears things up.

colucix 07-10-2010 03:08 PM

Quote:

Originally Posted by tank junior (Post 4029056)
What I need is a way where inside "run.sh", I can access the directory where it resides.

If this is the exact requirement, something like this should do the trick:
Code:

#!/bin/bash
MYDIR=$(dirname $(readlink -f $0))
echo $MYDIR

where $0 is the name of the script itself and readlink -f gives its full path, independently from the way you invoked script. The dirname command let you retrieve only the name of the directory where the script resides. Hope this helps.

tank junior 07-11-2010 12:50 AM

Hi colucix,

You are the man!!!!!
IT WORKS....

Thanks for the tip.

Cheers

Prashant


All times are GMT -5. The time now is 02:19 AM.