Creating Your Own Environment Variables

Programs use environment variables, thanks to the getenv() function shown in last week’s Lesson. They can also create their own environment variables, reset variable values, and remove variables.

To add your own environment variable, use the setenv() function, prototyped in the stdlib.h header file. Here is the man page format:

int setenv(const char *name, const char *value, int overwrite);

Strings name and value are strings representing the environment variable name and value to add. The overwrite option is zero or non-zero. If overwrite is zero and name already exists, it’s not overwritten. Otherwise, if name exists (and overwrite is non-zero) the new value replaces the original value.

Remember that the function returns zero on success even if overwrite is zero and name already exists. In this case, name isn’t overwritten, which the function considered a “success.”

2024_02_17-Lesson-a.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const char newvar[] = "language";
    const char value[] = "C";

    printf("Setting variable '%s' to '%s'\n",
            newvar,
            value
          );
    setenv(newvar,value,0);

    printf("Result: '%s'\n",getenv(newvar) );

    return 0;
}

This code sets the language variable to the value C in the environment: language=C If language already exists, it’s overwritten. Here’s a sample run:

Setting variable 'langauge' to 'C'
Result: 'C'

Another function similar to setenv() is putenv(), also prototyped in the stdlib.h header file. The difference between the two is that putenv() requires the entire string to be set at once. For example:

putenv("language=C");

Otherwise, both functions perform the same task.

One thing to keep in mind with environment variables is that any variables created or modified in a program are local to that program. When the program ends, the variables are lost. After running the program above, you won’t find language=C in the environment.

To deliberately remove created environment variables from your program. The unsetenv() function handles the job, as shown in this code:

2024_02_17-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const char newvar[] = "language";
    const char value[] = "C";

    printf("Setting variable '%s' to '%s'\n",
            newvar,
            value
          );
    setenv(newvar,value,0);

    printf("Result: '%s'\n",getenv(newvar) );

    printf("Freeing '%s'...",newvar);
    unsetenv(newvar);
    printf("done!\n");

    return 0;
}

The unsetenv() function’s sole argument is the environment variable to zap. Like setenv(), the function returns zero on success, -1 otherwise.

I’ve never written code that required an environment variable to created on-the-fly, or even to reset a variable’s value. A weather program I wrote relies upon the variable WXSTN to obtain the local weather station. But this variable is set when the shell starts, not in the program.

The environment is great for storing variables accessible when the program runs. For more detailed information, say longer than a string, use temporary or hidden files that contain the settings. Examples include the “dot” files used by many terminal window programs, often found lurking in the user’s home directory.

Leave a Reply