LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-05-2005, 02:40 PM   #1
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Rep: Reputation: 30
Scripts & Environment Variables


Hello.

This question came up while doing a school assignment, and nobody on campus seems to know the answer...

What I would like to be able to do is set some environment variables using a script... something like this

./myscript a b

where myscript contains:
export foo $1
export bar $2


However, these changes do not persist after the script terminates.

Is there any way to accomplish this?
It would save me a great deal of unnecessary typing

Last edited by geeman2.0; 07-05-2005 at 02:41 PM.
 
Old 07-05-2005, 02:52 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,972
Blog Entries: 11

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
source the script instead of running it

man bash
/source
 
Old 07-05-2005, 02:58 PM   #3
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,502

Rep: Reputation: 162Reputation: 162
"However, these changes do not persist after the script terminates.

Is there any way to accomplish this?"

In a word, no.

Variables can be set for the current process and subprocesses of the currecnt process. They cannot be set for a parent process. So in your example you enter a script name as a command in bash. Bash starts a subprocess to run the command. The subprocess can access variables inherited from its parent, make changes to those variables, and create new variables. When the subprocess ends, control Is passed back to the parent process and any variable changes made by the subprocess disappear. Upon return, the parent process' variables are exactly the same as they were before the subprocess was created.

If you want to make the variables universal I suggest that you create a config file and keep the variables there.

---------------------------
Steve Stites
 
Old 07-05-2005, 03:02 PM   #4
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,502

Rep: Reputation: 162Reputation: 162
I stand corrected. Tinkster's idea of running the script with the source command will work because the source command will run the command as the current process instead of a subprocess.

--------------------------
Steve Stites
 
Old 07-05-2005, 03:11 PM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
FYI, a shortcut for "source" is a ".".

For example, you should be able to do ". ./myscript a b".
 
Old 07-05-2005, 03:29 PM   #6
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Original Poster
Rep: Reputation: 30
Hmmm, is there any way to set these settings globally?

The problem is that it's a distributed systems assignment, and I'm running processes in different terminals that rely on environment variables that are constantly changing.

i.e. I have to frequently change an environment variable that stores port numbers.
It's not the way I would of designed the system, but the university wrote the project specifications :P
 
Old 07-05-2005, 03:33 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
You can set them on a per-user-login basis, using ~/.bash_profile, or on a system-wide per-login basis, using /etc/profile. There is no way to change the environment variables of another running process.
 
Old 07-05-2005, 03:37 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,972
Blog Entries: 11

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
A third alternative would be to put the script in the
/etc/profile.d directory instead of mucking about with
the substantial /etc/profile.


Cheers,
Tink
 
Old 07-05-2005, 03:40 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
How does a shell determine which scripts in /etc/profile.d to execute? In my /etc/profile.d, there are a bunch of tcsh scripts, but no bash scripts. Or do they try to execute any they find?
 
Old 07-05-2005, 03:52 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,972
Blog Entries: 11

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
Quote:
Originally posted by Matir
How does a shell determine which scripts in /etc/profile.d to execute? In my /etc/profile.d, there are a bunch of tcsh scripts, but no bash scripts. Or do they try to execute any they find?
Yep ...
Anything that's in there and executable should be sourced from
/etc/profile
Code:
From /etc/profile
--------8<--------8<--------8<--------8<--------8<--------8<--------
# Append any additional sh scripts found in /etc/profile.d/:
for file in /etc/profile.d/*.sh ; do
  if [ -x $file ]; then
    . $file
  fi
done
Slack makes good use of it :}
Code:
$ ls -l /etc/profile.d/
total 140
-rwxr-xr-x  1 root root   76 2003-11-14 19:45 ant.csh
-rwxr-xr-x  1 root root   75 2003-11-14 21:36 ant.sh
-rwxr--r--  1 root root  272 2004-08-12 12:25 bashrc.sh
-rwxr-xr-x  1 root root  164 2003-03-14 14:00 bsd-games-login-fortune.csh
-rwxr-xr-x  1 root root  141 2003-03-14 14:00 bsd-games-login-fortune.sh
-rwxr-xr-x  1 root root   32 2003-01-11 19:27 gtk+.csh
-rwxr-xr-x  1 root root   43 2003-01-11 19:28 gtk+.sh
-rwxr-xr-x  1 root root  102 2000-10-31 12:42 htdig.csh
-rwxr-xr-x  1 root root  101 2000-10-31 12:43 htdig.sh
-rwxr-xr-x  1 root root  146 2003-09-13 09:00 j2sdk.csh
-rwxr-xr-x  1 root root  145 2003-09-13 09:00 j2sdk.sh
-rwxr-xr-x  1 root root  176 2004-12-19 15:42 kde.csh
-rwxr-xr-x  1 root root   85 2004-12-19 15:42 kde.sh
-rwxr-xr-x  1 root root  227 2003-03-10 16:30 lang.csh
-rwxr-xr-x  1 root root 1199 2004-02-16 17:02 lang.csh.new
-rwxr-xr-x  1 root root  225 2003-03-10 16:31 lang.sh
-rwxr-xr-x  1 root root 1197 2004-02-16 17:00 lang.sh.new
-rwxr-xr-x  1 root root   51 2004-06-01 10:55 mc.csh
-rwxr-xr-x  1 root root   45 2004-06-01 10:55 mc.sh
-rwxr-xr-x  1 root root   31 2003-01-16 15:42 metacity.csh
-rwxr-xr-x  1 root root   31 2003-01-16 15:41 metacity.sh
-rwxr-xr-x  1 root root  117 2004-08-22 20:23 mozilla.sh
-rwxr-xr-x  1 root root   76 2005-03-01 19:58 netpbm.sh
-rwxr-xr-x  1 root root   57 2004-09-01 13:08 oo.sh
-rwxr-xr-x  1 root root  146 2004-09-02 15:52 oracle.sh
-rwxr-xr-x  1 root root   78 2004-07-04 19:33 pkg_conf.sh
-rwxr-xr-x  1 root root  148 2004-10-05 16:33 postgres.sh
-rwxr-xr-x  1 root root  148 2004-08-31 16:04 postgres.sh~
-rwxr-xr-x  1 root root  734 2004-10-14 16:19 qt.csh
-rwxr-xr-x  1 root root  642 2004-10-14 16:19 qt.sh
-rwxr-xr-x  1 root root  134 2000-04-25 07:46 tetex.csh
-rwxr-xr-x  1 root root  118 2000-04-25 07:46 tetex.sh
-rwxr-xr-x  1 root root  354 2005-02-13 18:38 xprint.csh
-rwxr-xr-x  1 root root  367 2005-02-13 18:38 xprint.sh
The ones that don't have a csh counterpart I added manually ;)

[edit]
Heh - I just saw that it's time for a house-cleaning again ;)

/me fires of a su -c "find / -iname \"*~\" -exec rm -f {} \;"
[/edit]


Cheers,
Tink

Last edited by Tinkster; 07-05-2005 at 03:57 PM.
 
Old 07-05-2005, 04:23 PM   #11
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
That's odd. While I know gentoo has a long history of doing things its own way, I find no references to profile.d in /etc/profile. In fact, the only ones I find are within /etc/profile.d itself, and a reference to /etc/profile.d/bash-completion in /etc/skel/.bashrc.

Seems a bit odd. I wonder if all distros (are supposed to) use profile.d.
 
Old 07-05-2005, 04:41 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,972
Blog Entries: 11

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
SuSE, and DeadRat seem to make use of it, I just verified that on
two servers I have access to. And the roll-your-own distro that's
in use here has it, too.


Cheers,
Tink
 
Old 07-05-2005, 05:10 PM   #13
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Odd. If any other gentoo users see this, can you confirm/deny use of profile.d?
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/etc/crontab & environment variables gepesz Linux - Software 3 09-03-2004 01:26 AM
Why can't my scripts set environment variables? Matthew3 Linux - Software 5 08-28-2004 05:26 PM
Need help setting right environment variables:configure scripts can't find libraries. z-vet Linux - Newbie 4 08-16-2004 05:10 PM
Environment Variables in Scripts louisb Linux - General 3 01-15-2004 04:56 PM
Environment Variables if you su concoran Linux - Newbie 1 08-16-2002 07:18 PM


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