LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 11-16-2009, 11:19 AM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Rep: Reputation: 30
shell to set environment variable


I am trying to set environment variables via a shell script.
However, after the script exits, the env variable is still not set.

Is there a way to do so?

This is my aa.sh

export DPATH=/var/www/html/www.julycenter.com
export CPATH=/var/www/html/www.julycenter.com/css
 
Old 11-16-2009, 11:24 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, FreeBSD
Posts: 3,925
Blog Entries: 5

Rep: Reputation: Disabled
I don't think so, because the shell script is a child of the shell.

If you need for the values to persist, either 1) export them in the parent environment instead; or 2) write them to some sort of temporary file from the child environment (where they can later be read by the parent).
 
Old 11-16-2009, 11:28 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,089

Rep: Reputation: 207Reputation: 207Reputation: 207
Hi

Try this:

source aa.sh

Then the variables will be set.
 
Old 11-16-2009, 11:31 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Do you have to use a script?

Running a script will not set these vars in the shell you started the script from.

Parsing a file with commands will set them in the shell you parsed in from.

. aa

aa being a file with just the 2 shown export commands in post #1.

Hope this helps.
 
Old 11-16-2009, 11:31 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Hi

Try this:

source aa.sh

Then the variables will be set.
or the short version
. script
 
Old 11-16-2009, 11:35 AM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, FreeBSD
Posts: 3,925
Blog Entries: 5

Rep: Reputation: Disabled
Guttorm's idea does work if aa.sh does not have an exit.

Code:
$ cat foo.sh 
#!/bin/sh

_myval='Hello'

. /home/mrbig/bar.sh

echo ${_myval}

exit 0
Code:
$ cat bar.sh 
#!/bin/sh

_myval='Goodbye'
Code:
$ ./foo.sh 
Goodbye
 
  


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
How to set an environment variable royeo Linux - Newbie 1 12-01-2006 12:59 AM
how to set the Environment variable from shell Scrit Sundaram Linux - Software 2 10-13-2006 11:59 PM
How to set a value of environment variable? ukrainet Linux - Newbie 1 12-28-2004 04:33 AM
How to set an environment variable? dantemarmol Linux - Newbie 7 08-22-2003 10:59 AM
how to set CC environment variable mr.moto General 2 12-18-2001 01:02 AM


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