Unix Technical Forum

How can I set an environment variable of the current shell?

This is a discussion on How can I set an environment variable of the current shell? within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi, No, don't pull out the "export FOO=BAR" answers just yet... I'm trying to set an environment variable of ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 07:17 AM
Jurjen Oskam
 
Posts: n/a
Default How can I set an environment variable of the current shell?

Hi,

No, don't pull out the "export FOO=BAR" answers just yet...

I'm trying to set an environment variable of the current shell process.
Using "export FOO=BAR" doesn't work, the variable FOO will then only
be set in the environment of child processes of the shell. An example:

First, verify $FOO isn't currently set as a shell variable:

$ export | grep -q FOO && echo found
$

Also verify it isn't part of the current environment:

$ ps geww $$ | grep -q FOO && echo found
$

So, neither $FOO is set, nor is FOO part of the environment. Now let's
set $FOO, and check again:

$ export FOO=BAR
$ export | grep -q FOO && echo found
found
$ ps geww $$ | grep -q FOO && echo found
$

So, $FOO is now set, but FOO still isn't part of the environment of the current
shell proces. When I start a new shell, this changes:

$ ksh
$ export | grep -q FOO && echo found
found
$ ps geww $$ | grep -q FOO && echo found
found
$

In the new process, FOO is set as an environment variable, and $FOO is set as
a shell variable.


I'm looking for a way to influence the environment of the current shell
process. In Perl, it's easy to influence the environment of the running
proces: $ENV{FOO}="BAR". I have the feeling I'm overlooking something quite
basic... It isn't specific to AIX ksh, the OpenBSD ksh behaves the same.
Doing an exec of ksh works, but that's more of a workaround. There must be
a cleaner solution.

If someone has a tip about how to influence the environment of the current
shell, that would be great...

--
Jurjen Oskam

Savage's Law of Expediency:
You want it bad, you'll get it bad.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 07:17 AM
Michael W. Ryder
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

Jurjen Oskam wrote:
> Hi,
>
> No, don't pull out the "export FOO=BAR" answers just yet...
>
> I'm trying to set an environment variable of the current shell process.
> Using "export FOO=BAR" doesn't work, the variable FOO will then only
> be set in the environment of child processes of the shell. An example:
>
> First, verify $FOO isn't currently set as a shell variable:
>
> $ export | grep -q FOO && echo found
> $
>
> Also verify it isn't part of the current environment:
>
> $ ps geww $$ | grep -q FOO && echo found
> $
>
> So, neither $FOO is set, nor is FOO part of the environment. Now let's
> set $FOO, and check again:
>
> $ export FOO=BAR
> $ export | grep -q FOO && echo found
> found
> $ ps geww $$ | grep -q FOO && echo found
> $
>
> So, $FOO is now set, but FOO still isn't part of the environment of the current
> shell proces. When I start a new shell, this changes:
>
> $ ksh
> $ export | grep -q FOO && echo found
> found
> $ ps geww $$ | grep -q FOO && echo found
> found
> $
>
> In the new process, FOO is set as an environment variable, and $FOO is set as
> a shell variable.
>
>
> I'm looking for a way to influence the environment of the current shell
> process. In Perl, it's easy to influence the environment of the running
> proces: $ENV{FOO}="BAR". I have the feeling I'm overlooking something quite
> basic... It isn't specific to AIX ksh, the OpenBSD ksh behaves the same.
> Doing an exec of ksh works, but that's more of a workaround. There must be
> a cleaner solution.
>
> If someone has a tip about how to influence the environment of the current
> shell, that would be great...
>


Have you tried setting the variable in .profile? If I set a variable in
..profile and then use 'set' to display the environment they show as the
value I used.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 07:17 AM
Joachim Gann
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

On 17 Apr., 22:59, Jurjen Oskam <jur...@stupendous.org> wrote:
> I'm trying to set an environment variable of the current shell process.
> Using "export FOO=BAR" doesn't work, the variable FOO will then only
> be set in the environment of child processes of the shell. An example:


a process can alter its own environment via the putenv() system call.
your shell just keeps its own list of variable/value pairs and puts
the exported ones into the environment of spawned child processes.
perl language designers have included a language element to allow you
to do putenv() whereas shell language designers seem to have not done
so.
but why would you care? what would a (hypothetical) "shell_putenv()"
give you that is superior to "FOO=BAR" or "export FOO=BAR" ?

Joachim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 07:17 AM
Joachim Gann
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

On 17 Apr., 22:59, Jurjen Oskam <jur...@stupendous.org> wrote:
> Hi,
>
> No, don't pull out the "export FOO=BAR" answers just yet...
>
> I'm trying to set an environment variable of the current shell process.
> Using "export FOO=BAR" doesn't work, the variable FOO will then only
> be set in the environment of child processes of the shell. An example:


a process can alter its own environment via the putenv() system call.
your shell keeps its own list of name/value pairs (seperate from its
own environment) and copies the exported ones to the environment of
spawned child processes.
perl language designers have included a language element to allow
scripts to use putenv() whereas shell language designers seem to have
not done so.
but why would you care? what would make a (hypothetical)
"shell_putenv" superior to a "FOO=BAR" or "export FOO=BAR"?

Joachim

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-21-2008, 06:45 AM
Jurjen Oskam
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

On 2008-04-18, Joachim Gann <joachim.gann@gmail.com> wrote:

> would you mind sharing the reason with us why "export FOO=bar" is not
> sufficient for you?


I already did, in slrng0h6ot.8am.jurjen@calvin.stupendous.org
(http://groups.google.com/group/comp....b546661b97a17).

Short version: I was trying to look at exported shell variables (thinking they
were environment variables) from other processes, and this doesn't work
when the shell doesn't actually put exported shell variables in the
environment of the current shell.

But it's no big deal, I'll find another way to achieve my original goal.
It just surprised me (and seeing the reactions, it surprised some other
people as well ), and I became curious.

--
Jurjen Oskam

Savage's Law of Expediency:
You want it bad, you'll get it bad.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-21-2008, 06:45 AM
Timothy J. Bogart
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

Jurjen Oskam wrote:
> On 2008-04-18, sjm <sjm_news@yahoo.co.uk> wrote:
>
>> The key to all this is that shell variables and environment variables
>> are in fact distinct and not the same. The shell is working on shell
>> variables and perl on the environment. Use the source luke ...

>
> True, but an afwul lot of documentation implies or outright states that
> a shell variable which is exported *is* an environment variable.
>
> For example, here's a piece of the official AT&T ksh manpage (emphasis mine):
>
> [...] The environment (see environ(7)) is a list of name-value pairs that is
> passed to an executed program in the same way as a normal argument list.
> [...] The shell interacts with the environment in several ways. On invocation,
> the shell scans the environment and creates a variable for each name found,
> giving it the corresponding value and marking it export. Executed commands
> inherit the environment. *If the user modifies the values of these variables
> or creates new ones, using the export or typeset-x commands they become part
> of the environment*. [...]
>
> (Also, using the source is a bit difficult on a closed-source OS such as AIX.)
>


Which is one of the many reasons having bash or ksh available as source
is so cool, eh what?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-21-2008, 08:40 AM
cococlyde55@gmail.com
 
Posts: n/a
Default Re: How can I set an environment variable of the current shell?

On Apr 18, 1:10 pm, Jurjen Oskam <jur...@stupendous.org> wrote:

> This indeed seems to be the case. Even when changing the value of an
> environment variable the shell only updates its own internal variable, so
> you end up with different values...
>
> Makes me wonder why this is the case, and it makes the term "environment
> variable" a bit ambiguous when the shell is involved.


export only marks variables for export to commands that will be
executed in the future
this allows the shell implementation to manage its variables (export
and non-export) much
more efficiently than most putenv(3) implementations

from the posix export man page:

The shell shall give the export attribute to the variables
corresponding to the specified names, which shall cause
them to be in the environment of subsequently executed
commands. If the name of a variable is followed by = word, then the
value of that variable shall be set to word.

-- Glenn Fowler -- AT&T Research, Florham Park NJ --
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:17 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com