0

C Program: (File name: temp.c)

main ()
{
  putenv ("VERSION");
  setenv ("VERTSION","1.0",1);
  printf ("%s\n\r",getenv ("VERSION");

  exit (0);
}

output of Cprogram: 1.0

==========================================================

Bash Script:

./temp
echo "Ver from bash:"$VERSION

output of bashscript:

1.0                        **(from c program)**
Ver from bash:             **(from bash shell, why wont $VERSION get output here?)**
  • IIRC, the environment used by a running program is just a copy of the environment at the point of execution - and it's discarded when the program finishes. There is probably an API to set the global environment, but I don't know how to point you to it. – theGleep May 31 '18 at 22:10
  • You C code doesn't even compile... – sticky bit May 31 '18 at 22:10
  • 1
    @theGleep A process can only change its own environment, not any other process. When you fork a child process it inherits a copy of the parent's environment. – Barmar May 31 '18 at 22:12
  • This is why programs like `tset` have to be executed using `eval`. They print the variable assignment statements, and the calling shell executes them with `eval`. – Barmar May 31 '18 at 22:15
  • @Bamar - so I was right in the main...but wrong about a possible workaround. Makes sense when you think about it... the environment belongs to the shell, and there are lots of different shells. Doesn't make sense to expect an API to set environment variables for *ALL* of the different shells – theGleep May 31 '18 at 22:33

0 Answers0