Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C getenv() function

Reference for C getenv(): read environment variables such as PATH or HOME, handle NULL, copy the result safely, and use setenv() and secure_getenv() on POSIX.

getenv() looks up an environment variable by name and returns a pointer to its value, or NULL if the variable does not exist. Environment variables are the classic way to pass settings such as HOME, PATH, TMPDIR or a debug flag from the shell to a program.

The returned string belongs to the environment. Treat it as read-only and copy it if you need it later.

C
#include <stdlib.h>

char *getenv(const char *name);

Try it

C Checking for NULL and using defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Setting, reading and parsing a variable (POSIX)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
char *getenv(const char *name);
Name Description
name The variable name, without an = sign. Names are case-sensitive on Unix; Windows ignores case.

Returns: a pointer to the value, or NULL if the variable is not defined. An existing but empty variable returns a pointer to an empty string, which is different from NULL.

Errors / undefined behavior: modifying the returned string is undefined behavior. The pointer may be invalidated by a later setenv, putenv, unsetenv or another getenv call (the standard allows getenv to reuse an internal buffer). Calling it concurrently with those functions is a data race.

Usage notes

  • Always test for NULL. printf("%s", getenv("X")) with an unset variable is undefined behavior (glibc prints (null), but you cannot rely on it).
  • Copy before keeping. Store the value in your own buffer or strdup it if it must outlive further environment changes.
  • Setting variables is not standard C. setenv, unsetenv and putenv come from POSIX (Windows has _putenv_s). A change only affects your process and the programs it starts later; it never changes the parent shell.
  • Do not trust the environment in privileged programs. An attacker controls variables such as PATH, LD_PRELOAD or IFS for setuid programs. glibc offers secure_getenv(), which returns NULL when the process runs with elevated privileges.
  • Not a place for secrets. On many systems other users can read a process's environment through /proc or process listings.
  • Listing all variables needs the POSIX global environ, which is not declared in headers; pass it through extern char **environ;.
  • Common variables: HOME, USER, PATH, TMPDIR, LANG; values differ per machine, so never print them in a test that must give the same output everywhere.