APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Passing shell environment variables


Passing shell environment variables


2013/07/25






Is there any way for a child process to set an environment variable visible to its parent?

The answer is both yes and no. Let's get the "no" out of the way first: no matter what you do, there is no way for a child process to affect the value of a variable in its parent. That said, it is possible for a parent process and a child process to cooperate and share whatever you need them to share.

So, if you have a script that says MYVAR="foo" and it calls another script that sets MYVAR="bar", $MYVAR remains "foo" in the parent script. The same idea is true if you change directories in the called script: when it exits, you won't be in the directory it changed to.

There are always ways to get what you need. One of the simplest is to NOT call the other script, but rather to execute it in-line, in the context of the current script. In most Unixy shells, you can use what's called "dot syntax". That is, instead of doing:

#!/bin/bash
MYVAR="foo"
otherscript
echo $MYVAR
 

you do this:

#!/bin/bash
MYVAR="foo"
. otherscript
echo $MYVAR
 

This way (with the ". otherscript") there won't be another process created. You can think of it like an "include". If "otherscript" changes MYVAR, that last echo will show the change.

A couple of other interesting things: "otherscript" does not need to be executable and if called as shown above, it would have to be in the current directory.

The C shells, /bin/csh, tcsh and others use the ``source'' command:

        source otherscript
 

But I hope you are not using any csh or variant!

You can use "eval" if "otherscript" does things like:

#!/bin/bash
echo "MYVAR=$PATH"
 

In that case, your "parent" script (it's not really a parent anymore) does this:

#!/bin/bash
MYVAR="foo"
eval `./otherscript`
echo $MYVAR
 

Other thoughts

If the "variable" can be handled with an integer, the exit status of the child is available to the parent. That's the usual way of learning why something terminated. So, if your child process wants to communicate "128" to its parent, you can do this:


#!/bin/bash
# "otherscript"
#whatever stuff it does
exit 128

#!/bin/bash
# Parent
MYVAR="foo"
./otherscript
MYVAR=$?
echo $MYVAR
 

Pipes

If not (more data needs to be passed), a shared named pipe is a little better than a file (and easier to handle concurrency with) , shared memory is the fastest (but also more overhead to setup). You can also just setup a bidirectional pipe between two programs; this can even be across a network if necessary.

*Exactly* what happens with pipes depends on the shell. But the differences are very minor- a job control shell vs. a non job control shell will assign process groups differently. Under ordinary circumstances that is so unimportant to you that I probably shouldn't even mention it.

Pipes block when full. How much data you can write is (for example) examined for bash with "ulimit -p" , which is the size of the pipe in 512 byte blocks and the bash man page plainly tells you that it is a read-only value (you can't make it larger).

However, a named pipe provides communication between two otherwise unrelated proceesses. One doesn't have to call the other, or even know its name, and the one side of the pipe doesn't even have to be running when the other side runs. As a practical example, I often run across stupid programs that think they have to print to a device and don't know how to use the spooler. That's fine until we want to make the printer into a network printer. The solution is to make a named pipe and tell the dumb program that the pipe is its print device. You start another process that does this:

while :
do
exec </dev/myfakenetprint
lpr -P myrealnetprinter
done
 

Then when the program writes to the pipe /dev/myfakenetprint, it actually goes to the spooler.



If this page was useful to you, please help others find it:  





Comments?




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!


Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Linux
       - MacOSX
       - Scripting
       - Shell















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)