NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |SEE ALSO |COLOPHON | |
putenv(3) Library Functions Manualputenv(3)putenv - change or add an environment variable
Standard C library (libc,-lc)
#include <stdlib.h>int putenv(char *string); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):putenv(): _XOPEN_SOURCE || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE
Theputenv() function adds or changes the value of environment variables. The argumentstring is of the formname=value. Ifname does not already exist in the environment, thenstring is added to the environment. Ifname does exist, then the value ofname in the environment is changed tovalue. The string pointed to bystring becomes part of the environment, so altering the string changes the environment.
Theputenv() function returns zero on success. On failure, it returns a nonzero value, anderrno is set to indicate the error.
ENOMEMInsufficient space to allocate new environment.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────┬───────────────┬─────────────────────┐ │Interface│Attribute│Value│ ├──────────────────────────┼───────────────┼─────────────────────┤ │putenv() │ Thread safety │ MT-Unsafe const:env │ └──────────────────────────┴───────────────┴─────────────────────┘
POSIX.1-2008.
POSIX.1-2001, SVr2, 4.3BSD-Reno. Theputenv() function is not required to be reentrant, and the one in glibc 2.0 is not, but the glibc 2.1 version is. Since glibc 2.1.2, the glibc implementation conforms to SUSv2: the pointerstring given toputenv() is used. In particular, this string becomes part of the environment; changing it later will change the environment. (Thus, it is an error to callputenv() with an automatic variable as the argument, then return from the calling function whilestring is still part of the environment.) However, from glibc 2.0 to glibc 2.1.1, it differs: a copy of the string is used. On the one hand this causes a memory leak, and on the other hand it violates SUSv2. The 4.3BSD-Reno version, like glibc 2.0, uses a copy; this is fixed in all modern BSDs. SUSv2 removes theconst from the prototype, and so does glibc 2.1.3. The GNU C library implementation provides a nonstandard extension. Ifstring does not include an equal sign: putenv("NAME"); then the named variable is removed from the caller's environment.clearenv(3),getenv(3),setenv(3),unsetenv(3),environ(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17putenv(3)Pages that refer to this page:clearenv(3), getenv(3), pam_putenv(3), setenv(3), proc_pid_environ(5), environ(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |