0

I want to set a path environment variable in bash by C program. So I coded using 'setenv' function, But it was not the answer to solve.

Could anybody suggest another way to solve this problem in C programming?

I thought the other solution that the program read the profile file, then modify and save, but actually when I opened this file there's no string I wanted about PATH variable.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • If you change the PATH inside the C program, that value is lost when the C program exits. So if you want the change to persist into a shell, you will need to have your program exec a shell (so the shell becomes either a child of the original program, or it becomes the process that was running the original program; either way, it will inherit the environment that your program modifies.) – William Pursell Mar 25 '15 at 15:59
  • Maybe you want some sort of `system("export PATH=something+$PATH");`? – ForceBru Mar 25 '15 at 16:00
  • Welcome to SO. I'm afraid I do not understand what problem you are trying to solve. Could you describe (by editing your question, not in a comment) the end effect you want to achieve, such as “I want to call this command in a shell and then my `$PATH` in that shell should be changed”? – Christopher Creutzig Mar 25 '15 at 16:06
  • don't exactly know why you want to do this. but it sounds to me it's a better to have a launch script instead or call it like `PATH=$PATH:new_path ./your_program`. – Jason Hu Mar 25 '15 at 16:13
  • thanks for read my question @ForceBru I tried but it just only works in my process. if the program exit, then It doesn't work any... – Choi Taekyoon Mar 25 '15 at 16:38

2 Answers2

7

You can use setenv() and putenv() to set environment variables. But these will only be set for the given program. You cannot set environment variables for the shell or its parent process.

Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
  • @EugeneSh. But that still won't set the environment variables for the parent *process*. You'd have a launch a new shell or manually `source` those files for the changes to take effect, which defeats the purpose of trying to do it in a subprocess. – Crowman Mar 25 '15 at 17:39
  • @PaulGriffiths It can be done together with `setenv`, but I agree it is unlikely to be a solution, just an ugly workaround. – Eugene Sh. Mar 25 '15 at 17:41
  • @EugeneSh. But `setenv` also will not affect the parent process, only the calling process. – Crowman Mar 25 '15 at 17:42
-1

Here an example to define the Python path.

It is created a string path and append it to the python path.

char *append_path = malloc(sizeof(char) * 1000);
append_path = "/trunk/software/hmac_sha256/:.";
printf("Append to path is:\n%s\n", append_path);           
setenv("PYTHONPATH",append_path,1);//Set PYTHONPATH TO working directory https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/setenv.htm
char *path = Py_GetPath();
printf("Python search path is:\n%s\n", path);

This should append the string to the PYTHONPATH environment variable. For me it is working as stated before. In case the variable is replaced and not appended, then you just need to read the environment variable before, append the new string and then do the "setenv".

for example

//include string functions
#include <string.h>

....

char *current_path = malloc(sizeof(char) * 1000);
current_path = Py_GetPath();
printf("Current search path is:\n%s\n", current_path);
char *new_path = malloc(sizeof(char) * 1000);
new_path = "/trunk/software/hmac_sha256/:.";
printf("New to add path is:\n%s\n", new_path);
snprintf(current_path, sizeof(char) * 1000, "%s%s", current_path,new_path);//this concatenate both strings
setenv("PYTHONPATH",current_path,1);//Set PYTHONPATH TO working directory https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/setenv.htm
char *path = Py_GetPath();
printf("Python search path is:\n%s\n", path);
Joniale
  • 515
  • 4
  • 17