It is currently Tue, 17 May 2022 02:43:35 GMT

Author Message
 Exporting Enviroment Variables in shell scripts
This is probably an easy question, but I was wondering, if there would be
any reason why you wouldn't be able to export an enviroment variable (such
as  in a shell script.)Please help me out, Im a newbie at this so I would
greatly appreciate any suggestions or answers. Thanks.

 Wed, 26 Jan 2005 22:22:37 GMT   
 Exporting Enviroment Variables in shell scripts

    It's my understanding that you cannot export them to
the parent shell.  Your script creates a subshell (another
copy of  whatever shell you are using) and any variables
die with it.
    Hope I'm wrong. ( I'm a newbie too, and this one has bugged me
thoroughly) but the docs are pretty clear.


 Wed, 26 Jan 2005 23:33:00 GMT   
 Exporting Enviroment Variables in shell scripts
In article <Mma59.10878$>,

Yes, that's correct.  If you want to pass information from child process to
parent, you must use some mechanism to do that explicitly, typically a pipe.
Or, as with the cd question, instead of using a script use an alias or

John DuBois  KC6QKZ/AE

 Thu, 27 Jan 2005 00:00:03 GMT   
 Exporting Enviroment Variables in shell scripts


    A pipe too....Good.  How about an example for the
3  approaches,  really simple ones?  I'd copy them into
my Bash directory ....

    For instance:  I have a  script that  cds to a directory and allows me
to run less on any file there.But when I  ^c  I find myself in the directory
where I
ran the script from.,  rather than the new one. ( You
explained why on the other thread.)  I'd LIKE to exit
into the one I'm in......You would laugh your head off if
you saw the number of  idiotic attempts I've made to
accomplish this...  Some of them almost crashed my


- Show quoted text -

 Thu, 27 Jan 2005 04:27:18 GMT   
 Exporting Enviroment Variables in shell scripts

Possibility number 1: a subshell (well, any subprocess) can pass data to
its parent by printing it to standard output.

Look at the shell function that ships with mc (Midnight Commander)?

When you run 'mc -P', mc prints out the cwd when you exit it. So, if you
want to use mc to change the working directory of your shell, you could
just define a function:

                cd "`mc -P`"

That's it!

Well, to be truthful, the above shell function, although simple and
concise, will behave very badly if you would happen to suspend mc (hit
ctrl-z). To work around the problem, you should save the output of 'mc
-P' into a file. Midnight Commander ships with this sample shell
function that you can use.

        mc ()
                mkdir -p $HOME/.mc/tmp 2> /dev/null
                chmod 700 $HOME/.mc/tmp
                /usr/bin/mc -P "$@" > "$MC"
                cd "`cat $MC`"
                /bin/rm -f "$MC"
                unset MC;

The script also takes care of passing other command line arguments
correctly to mc (the use of "$@") and allows you to run multiple mc's at
the same time (the temp file name includes the process id ($$)).

Usually you do not have to take excessive precautions like this and it
is enough to just run the subprocess inside backticks.

 Thu, 27 Jan 2005 05:03:59 GMT   
 Exporting Enviroment Variables in shell scripts

    Thank you, Sammi.  Ironically,  I am trying to write
a script for the console that will  act as a simple substitite
for mc and/or the file explorer in vim,  permitting me to
remove mc and vim and use vi.....
    But the key must be in the scripts above, and I will study them


 Thu, 27 Jan 2005 06:56:12 GMT   
 Exporting Enviroment Variables in shell scripts
each process will has its own enviroment variables. Once a process is
it's enviroment variables can't be changed by other process. And if you run
shell script in a shell, the shell will fork a new shell process to run the
sript, and the enviroment in the script is the new forked shell, but not the
parant shell. If you would like to change the current shell enviroment,
you should let the current shell to run the script itself.

for csh: soure yourscript
for sh, bash: . yourscript


 Wed, 26 Jan 2005 22:59:16 GMT   
   [ 7 post ] 

Similar Threads

1. export variables to calling shell in a shell-script

2. export variables to calling shell in a shell-script

3. Setting enviroment variables in a shell script

4. file contents into variable, or global variable from shell script

5. Shell Variables: Shell prompt OK, script, NO.

6. Passing a variable from a Perl script to a shell script

7. Passing a variable between shell script and perl script

8. make - exporting shell variable to environment

9. exporting C shell variable

10. how do I export a shell variable

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software