0

EDIT

Unfortunately, getenv or setenv do not modify the path. When I used the following code

int main() {
    char *oldenv = strdup(getenv("PATH")); // Make a copy of your PATH
    setenv("PATH", "$PATH:~/opt/bin", 1); // Overwrite it

    system("echo $PATH"); // Outputs "$PATH:~/opt/bin"

    setenv("PATH", oldenv, 1); // Restore old PATH
    free(oldenv); // Don't forget to free!

    system("echo $PATH"); // Outputs your actual PATH
}

the codes prints out the hard-coded $PATH:~/opt/bin instead of the usual $PATH command.

End of Edit

I found that this link provides how to add the file name in $PATH but not in c programming language. When I run the codes, it simply prints out the original path instead of the modified path. Interestingly, when I put system("echo $PATH:~/opt/bin"), it successfully show the modified path. Also, I'm not sure how to remove the same file name (~/opt/bin) from the modified path.

My codes are here:

int main (void) {

  system("echo $PATH");
  system("export PATH=$PATH:~/opt/bin");
  system("echo $PATH"); //prints out the original $PATH

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 3
    You can't use `$PATH` or `~/opt/bin` when calling `setenv()`, sorry. Neither `$` nor `~` will be expanded as those are shell features and there's no shell here. You'll have to implement them yourself with C code: read the current `PATH`, use `strcat()` or similar to append to it, and write out the path to `/home/username/opt/bin` instead of using `~` as shorthand. If you don't know the current user's path then you'll also need to [look that up](https://stackoverflow.com/a/26696759/68587). – John Kugelman Apr 15 '20 at 00:37
  • 2
    Note that in the second fragment, your second shell sets its PATH, but the value is thrown away when that shell exits, so the third `system` call will produce the same output as the first under all normal circumstances. – Jonathan Leffler Apr 15 '20 at 04:02
  • 1
    Also note that doing this in a C program won't affect the shell that invokes the C program — unless you do something to capture printed output and use it to set PATH in the shell's environment: `export PATH=$(./your_command)` where your command produces the new value for PATH. Beware of losing the old value of PATH while testing. – Jonathan Leffler Apr 15 '20 at 04:10
  • 1
    @SungminLim : In your edited script, you create three different child processes. Each process inherits the PATH from the parent process (which is your C program). But you never modify the `PATH` environment variable in your C program. Therefore both `echo` commands output the same value. You can use the C function `setenv`, to change your environment, and the changed environment will be inherited by the child processes. – user1934428 Apr 15 '20 at 05:55
  • I used getenv("PATH") to save the path in a string. All these comments helped me to understand computer programming better, and I appreciate for your useful comments. –  Apr 15 '20 at 21:44

0 Answers0