LinuxQuestions.org
Review your favorite Linux distribution.
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 07-10-2010, 04:49 AM   #1
tank junior
Member
 
Registered: Apr 2010
Posts: 42

Rep: Reputation: 0
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
 
Old 07-10-2010, 04:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

The pwd command shows the current working directory.

export CURRENTDIR=`pwd`

Hope this helps.
 
Old 07-10-2010, 05:17 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,354
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
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
 
Old 07-10-2010, 05:25 AM   #4
tank junior
Member
 
Registered: Apr 2010
Posts: 42

Original Poster
Rep: Reputation: 0
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
 
Old 07-10-2010, 05:29 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
@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
 
Old 07-10-2010, 05:39 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
 
Old 07-10-2010, 05:41 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
There's already a variable that holds the present working directory. Oddly enough, it's "$PWD".
 
Old 07-10-2010, 07:38 AM   #8
tank junior
Member
 
Registered: Apr 2010
Posts: 42

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

Last edited by tank junior; 07-10-2010 at 07:48 AM.
 
Old 07-10-2010, 11:43 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
 
Old 07-10-2010, 03:08 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by tank junior View Post
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.
 
1 members found this post helpful.
Old 07-11-2010, 12:50 AM   #11
tank junior
Member
 
Registered: Apr 2010
Posts: 42

Original Poster
Rep: Reputation: 0
Hi colucix,

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

Thanks for the tip.

Cheers

Prashant
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variable containing the current keyboard layout _ON Linux - Newbie 1 05-18-2010 11:04 PM
How to set an environment variable royeo Linux - Newbie 1 12-01-2006 12:59 AM
How to set a value of environment variable? ukrainet Linux - Newbie 1 12-28-2004 04:33 AM
Setting environment variable to current date/time nafan Programming 1 08-23-2004 09:04 PM
edit path environment variable to include current working directory alb1954 Linux - Newbie 8 07-11-2004 11:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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