Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How do I set a Linux environment variable from within C/C++

Posted on 2004-08-07
Medium Priority
Last Modified: 2008-01-09

I wish to set a Linux environment variable from within GNU C/C++.

I've tried "system("var=hello"); but this doesnt work - the variable seems to disappear when I check it in a shell script.
Question by:Risky101
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Expert Comment

ID: 11745516
man setenv

       #include <stdlib.h>

       int setenv(const char *name, const char *value, int overwrite);
LVL 51

Expert Comment

ID: 11748753
the variable seems to disappear when I check it in a shell script
you cannot set an variable from within your program which is still alive after this program exits
The only way to do this is to exit your program with any of the exec*() function, calling another program or shell

Expert Comment

ID: 11750100
i would agree with ahoffmann,  env variables can be passed on to children processes, but not escalated up to parents., hence in the C program, whatever u set , will die once the program dies.. unless u exec some shell/program from there in.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 11754023
the program 'read' (built into Bash) manages to set the environment variable properly, ie:

read yesno
if [ "$yesno" = "y" ];then
  # do something

So far, I've tried:
system ("export etc etc");
system ("declare etc etc");

Without exception, the variables disappear when the program exits. Is there any way, any at all, to set an environment variable, just like read does?

LVL 51

Expert Comment

ID: 11754193
>  the program 'read' (built into Bash) manages to set the environment variable properly, ie:

> Without exception, the variables disappear when the program exits.
yes, as already said.
LVL 20

Accepted Solution

jmcg earned 2000 total points
ID: 11754319
Environment variables are process-based. A process can only change environment variables for itself and, by inheritance, for its children.

Any invocation of 'system' creates at least one additional process. Any changes that process makes to its environment are lost when it exits and most certainly cannot effect the environment variables of the parent.

A subprocess can produce, as an output, an instruction for changing the parent's environment variable. And as you've experienced with the 'read' built-in, the shell can change its own environment variables and pass them along to its children.

So, the usual way to have a program return a value into a shell environment variable looks something like this:

export result

You can also have the program write its new settings for environment variables into a file

external-command >new-env

where the new-env file ends up looking like:

export env1
export env2

and the shell can then use the "." command to read in the new-env file to make the settings effective

. new-env


None of this is particular helpful if the only thing you have control over is a C/C++ program. The shell that calls your program has to cooperate if its environment variables are to be changed.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question