Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2010, 10:28 PM   #1
LQ Newbie
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Setting up environment variables in a C programming and accessing via a script

This is my first post and I thank people in advance for helping me find some answers.

I need to write a script and a C program. The script will execute the C program. The C program will setup a set of environment variables that will be used by the script. I am trying to figure out how to write the C program... I've used the setenv in the C program but when I echo the variable from the script it is empty. I also read that the setenv call sets up variables from the parent process forward. This makes me think that accessing the variable from the script won't work.

Anyways, this is what I am doing in the C program:

setenv("ENV_VAR", str, 1);

Then in the bash script I am doing this:

echo $ENV_VAR

Nothing is printing out. Any ideas or suggestions?
Thanks again,
Old 02-16-2010, 10:44 PM   #2
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
You won't be able to do it that way, environment variables are inherited from the parent process. If your script calls an executable, then that executable is run as a separate process and it can modify it's own environment, but when it exits, those settings are simply gone.

If your script sources another script, then that's a different matter. The second script is simply 'read in' not executed.

Old 02-16-2010, 10:54 PM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You are the 17th bzillionth person to try that, and it will always fail. The environment of a process is private to that process. Any child processes will inherit a copy of the environment, but even this doesn't help you in your unconventional/backward scenario. If you could do what you are trying to do, where would the limit be drawn for what process would be affected by changing the environment? Ultimately, in the Linux process model, all process are children of the init process, so imagine the far reaching effects you could have.
Your plan of having a C program create environment variables is highly unconventional. Normally, a shell is used to set up the environment, and a binary object module (your compiled C code) would inherit those environment variables. Probably your best bet would be to simply emit a string that can be `backticked` by the shell, which sets the required environment variables. Example:
#! /bin/sh
# Your program is called LQmateo14, and it prints to stdout:
# "ONEVAR=one2three\n"
# "TWOVAR=two3four\n"
echo $ONEVAR
echo $TWOVAR
--- rod.

Last edited by theNbomr; 02-16-2010 at 10:56 PM.


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing environment variables in startup scripts sanjeevkchopra Linux - Security 2 07-10-2006 10:16 AM
Accessing environment variables in startup scripts sanjeevkchopra Linux - Networking 1 07-10-2006 09:54 AM
Need help setting environment variables via shell script srosburg Linux - Newbie 2 12-08-2005 07:58 PM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 08:50 PM
Setting environment variables from a script... sylvain_gnu Linux - Newbie 5 04-20-2004 12:31 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration