Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the environment variable is unset after using setenv( )

Tags:

c

linux

I wrote a C program in Linux to set the values of environment variables using setenv, but after execution, when I run set or export, the environment variable itself seems to be unset. Why?

Here is the code snippet:

int main()
{
  char *mallocPtr, *callocPtr, *reallocPtr, *memalignPtr, *vallocPtr;
  struct sigaction sa;

  sa.sa_handler=SIGSEGV_handler;
  sigaction(SIGSEGV, &sa, NULL);

  if(setenv("ENV1", "3", 1) == 0)
         printf("ENV1 set to 3\n");
  else
         fprintf(stderr, "setenv failed on ENV1");
like image 726
RajSanpui Avatar asked Aug 08 '11 10:08

RajSanpui


People also ask

How do I set environment variables permanently?

To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as . bashrc , and change the ones that are already there or enter the new ones. When you create new users, these /etc/skel files will be copied to the new user's home directory.

What is the use of Setenv command?

The SETENV command can be used to define an environment variable and assign a value to it. The value of an environment variable can be retrieved from within the SAS session using the SYSGET function during autoexec processing. The command x setenv a/tmp; sets a=/tmp . The command x echo $a; results in the value /tmp.

What are environment variables How is it set modified and unset?

Environment Variables are some special variables that are defined in shell and are needed by programs while execution. They can be system defined or user defined. System defined variables are those which are set by system and are used by system level programs.


1 Answers

The environment variables are set within the context of your program.

When your program exits, you're back in the context from where your program was started.

like image 113
Didier Trosset Avatar answered Oct 10 '22 14:10

Didier Trosset