Is there a way to set environment variables in Linux using C?
I tried setenv() and putenv(), but they don’t seem to be working for me.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m going to make a wild guess here, but the normal reason that these functions appear to not work is not because they don’t work, but because the user doesn’t really understand how environment variables work. For example, if I have this program:
And then I run it from the shell, it won’t modify the shell’s environment – there’s no way for a child process to do that. That’s why the shell commands that modify the environment are builtins, and why you need to
sourcea script that contains variable settings you want to add to your shell, rather than simply running it.