1putenv(3) Library Functions Manual putenv(3)
2
3
4
6 putenv - change or add an environment variable
7
9 Standard C library (libc, -lc)
10
12 #include <stdlib.h>
13
14 int putenv(char *string);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 putenv():
19 _XOPEN_SOURCE
20 || /* glibc >= 2.19: */ _DEFAULT_SOURCE
21 || /* glibc <= 2.19: */ _SVID_SOURCE
22
24 The putenv() function adds or changes the value of environment vari‐
25 ables. The argument string is of the form name=value. If name does
26 not already exist in the environment, then string is added to the envi‐
27 ronment. If name does exist, then the value of name in the environment
28 is changed to value. The string pointed to by string becomes part of
29 the environment, so altering the string changes the environment.
30
32 The putenv() function returns zero on success. On failure, it returns
33 a nonzero value, and errno is set to indicate the error.
34
36 ENOMEM Insufficient space to allocate new environment.
37
39 For an explanation of the terms used in this section, see at‐
40 tributes(7).
41
42 ┌────────────────────────────────┬───────────────┬─────────────────────┐
43 │Interface │ Attribute │ Value │
44 ├────────────────────────────────┼───────────────┼─────────────────────┤
45 │putenv() │ Thread safety │ MT-Unsafe const:env │
46 └────────────────────────────────┴───────────────┴─────────────────────┘
47
49 POSIX.1-2008.
50
52 POSIX.1-2001, SVr2, 4.3BSD-Reno.
53
54 The putenv() function is not required to be reentrant, and the one in
55 glibc 2.0 is not, but the glibc 2.1 version is.
56
57 Since glibc 2.1.2, the glibc implementation conforms to SUSv2: the
58 pointer string given to putenv() is used. In particular, this string
59 becomes part of the environment; changing it later will change the
60 environment. (Thus, it is an error to call putenv() with an automatic
61 variable as the argument, then return from the calling function while
62 string is still part of the environment.) However, from glibc 2.0 to
63 glibc 2.1.1, it differs: a copy of the string is used. On the one hand
64 this causes a memory leak, and on the other hand it violates SUSv2.
65
66 The 4.3BSD-Reno version, like glibc 2.0, uses a copy; this is fixed in
67 all modern BSDs.
68
69 SUSv2 removes the const from the prototype, and so does glibc 2.1.3.
70
71 The GNU C library implementation provides a nonstandard extension. If
72 string does not include an equal sign:
73
74 putenv("NAME");
75
76 then the named variable is removed from the caller's environment.
77
79 clearenv(3), getenv(3), setenv(3), unsetenv(3), environ(7)
80
81
82
83Linux man-pages 6.05 2023-07-20 putenv(3)