Unix Shell - Is there an "undo" for export at the end of a shell script?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > Is there an "undo" for export at the end of a shell script?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Is there an "undo" for export at the end of a shell script?
Ulf Meinhardt

2005-02-17, 5:54 pm

Assume I change an environment variable at the beginning of a shell script e.g.

export BLA=myval:$BLA

Notice that $BLA already exists and has an unknown content. How do I switch back
to the original value at the end of the shell execution?

At a first glance I could store the initial value in a local variable:

savebla=$BLA
export BLA=myval:$BLA
.....
....other manipulations of $BLA
.....
(*1)
.....
export BLA=$savebla
exit


But what happens if the shell crashes at (*1) ?
The env variable has no chance to get back its original value.

Ulf

Chris McDonald

2005-02-17, 5:54 pm

ulf2m@email.com (Ulf Meinhardt) writes:

>Assume I change an environment variable at the beginning of a shell script e.g.


>export BLA=myval:$BLA


>Notice that $BLA already exists and has an unknown content. How do I switch back
>to the original value at the end of the shell execution?
>At a first glance I could store the initial value in a local variable:


>savebla=$BLA
>export BLA=myval:$BLA
>....
>...other manipulations of $BLA
>....
>(*1)
>....
>export BLA=$savebla
>exit


>But what happens if the shell crashes at (*1) ?
>The env variable has no chance to get back its original value.



If the shell crashes, then your shellscript will terminate (hopefully
with failure), so there is no meaning to "switch back to the original value...".

________________________________________
______________________________________
Dr Chris McDonald E: chris@csse.uwa.edu.au
Computer Science & Software Engineering W: http://www.csse.uwa.edu.au/~chris
The university of Western Australia, M002 T: +618 6488 2533
Crawley, Western Australia, 6009 F: +618 6488 1089
Chris F.A. Johnson

2005-02-17, 5:54 pm

On Thu, 17 Feb 2005 at 21:35 GMT, Ulf Meinhardt wrote:
> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?


There's no need; once the shell dies, the change dies with it.

> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.


The change is only effective in that shell or processes spawned by
it. Whether the script finishes or noy, it will not affect the
environment of the shell that called it (unless it was sourced).

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Barry Margolin

2005-02-18, 2:48 am

In article <cv32mg$js5$05$1@news.t-online.com>,
ulf2m@email.com (Ulf Meinhardt) wrote:

> Assume I change an environment variable at the beginning of a shell script
> e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch
> back
> to the original value at the end of the shell execution?


Why do you need to do this? Nothing you do in the shell script has any
effect on the parent process's environment.

Export makes variables get inherited by *child* processes that are
spawned from the script, they aren't inherited the other way.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Alan D Johnson

2005-02-18, 2:48 am

Ulf Meinhardt wrote:
> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>
> Ulf
>

exit routine
export BLA=
Alexander Skwar

2005-02-18, 2:48 am

· Ulf Meinhardt <ulf2m@email.com>:

> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?


You don't need to. The script is run in a subshell. Subshells cannot
influence env. vars of parent shells.

Given the following script:

#!/bin/sh
export FOO=bar
echo FOO in Script: $FOO
exit

When you do:

FOO=blubb
../mini-script.sh
echo $FOO

> The env variable has no chance to get back its original value.


Yes, it does.

Alexander Skwar
--
(null cookie; hope that's ok)
·_·¯·_·¯·_ ♘♞♟♙♖♜ аäöüßÄÖÜæœłø¼½¾¤¹²³¢
€£¥¶§¬÷×±©®™¡¿ ♪♬♯♪♬♯â™_ ¯·_·¯·_·¯·


AT

2005-02-18, 7:49 am

On Thu, 17 Feb 2005 22:35:12 +0100, Ulf Meinhardt wrote:

> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.


I may be wrong, but aren't variables local to the running shell, in this
case the shell under which the script is running (hint:#!/bin/bash)? This
will not influence the variables set in your login shell.

HTH
Andreas


Jan Kandziora

2005-02-18, 7:49 am

Ulf Meinhardt schrieb:

> Assume I change an environment variable at the beginning of a shell script
> e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I
> switch back to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>

Environment variables are specific to a process and can be "exported" to the
process' childs. There is no way a process can change the environment of
the process *it was called by*, so it is *not necessary* to "unset" any
environment variables before exit. That naturally also applies to processes
which run wild and break right in the middle.

Please read a book about shell scripting to learn more.

Note: This does not apply to shell scripts executed on behalf of the current
shell process via the "." shell builtin.
--
Jan
Chuck Dillon

2005-02-20, 6:19 pm

Ulf Meinhardt wrote:
> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution? >
> <snip>
>
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.


The environment is per process and (for all practical purposes) only
visible to that process. A copy is passed to its children. Once the
process crashes or exits it no longer has an environment. So you don't
need to do this.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Bill Marcum

2005-02-20, 6:19 pm

On Thu, 17 Feb 2005 22:35:12 +0100, Ulf Meinhardt
<ulf2m@email.com> wrote:
> Assume I change an environment variable at the beginning of a shell
> script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I
> switch back to the original value at the end of the shell execution?
>

Not necessary. The script runs in a subshell; it receives a copy of the
parent environment.
>

Toni Erdmann

2005-02-20, 6:19 pm

Ulf Meinhardt schrieb:
> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>
> Ulf
>


Don't worry at all!

The value of the modified variable will not be exported to the
calling process (shell), obly to programms called by your script

Except: if you call your shell script like this

.. myscript # read: dot blank myscript

The all variables (with their new/modified values) will be exported.

Toni
Doug Laidlaw

2005-02-20, 6:19 pm

Ulf Meinhardt wrote:

> Assume I change an environment variable at the beginning of a shell script
> e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I
> switch back to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>
> Ulf


I stand to be corrected, but it is my understanding that variables set by a
script are not set permanently, but your ordinary settings will prevail
when you are working outside the script, no matter how it was terminated.
The variable will revert to whatever value is assigned by your .bashrc, or
whatever. Otherwise any script that hangs and is terminated by Ctrl-C
would still affect things.

Doug.
--
ICQ Number 178748389. Registered Linux User No. 277548.
People want economy and they will pay any price to get it.
- Lee Iacocca.

Alan D Johnson

2005-02-21, 2:48 am

Ulf Meinhardt wrote:
> Assume I change an environment variable at the beginning of a shell script e.g.
>
> export BLA=myval:$BLA
>
> Notice that $BLA already exists and has an unknown content. How do I switch back
> to the original value at the end of the shell execution?
>
> At a first glance I could store the initial value in a local variable:
>
> savebla=$BLA
> export BLA=myval:$BLA
> ....
> ...other manipulations of $BLA
> ....
> (*1)
> ....
> export BLA=$savebla
> exit
>
>
> But what happens if the shell crashes at (*1) ?
> The env variable has no chance to get back its original value.
>
> Ulf
>

exit routine
export BLA=
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com