SETENV(3POSIX)
π SETENV β add or change environment variable π
okay so bestie, welcome to the POSIX Programmer's Manual β¨ this little page lives in that set, so heads up: the Linux implementation of this interface may differ (go peek at the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not even be implemented on Linux. not me warning you upfront π
πΈ PROLOG πΈ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
β¨ NAME β¨
setenv β add or change environment variable
π¦ SYNOPSIS π¦
#include <stdlib.h>
int setenv(const char *envname, const char *envval, int overwrite);
π DESCRIPTION π
ok so the setenv() function shall update or add a variable in the environment of the calling process. main character behavior fr.
The envname argument points to a string containing the name of an environment variable to be added or altered. The environment variable shall be set to the value to which envval points. the function shall fail if envname points to a string which contains an '=' character. like, that's the line, bestie, don't cross it.
If the environment variable named by envname already exists and the value of overwrite is non-zero, the function shall return success and the environment shall be updated. If the environment variable named by envname already exists and the value of overwrite is zero, the function shall return success and the environment shall remain unchanged. so like, zero means "hands off, she's already there" π«Ά
The setenv() function shall update the list of pointers to which environ points.
The strings described by envname and envval are copied by this function.
π heads up: the setenv() function need not be thread-safe.
π RETURN VALUE π
Upon successful completion, zero shall be returned. Otherwise, -1 shall be returned, errno set to indicate the error, and the environment shall be unchanged. zero = slay, -1 = not slay, and nothing got touched either way.
π ERRORS π
The setenv() function shall fail if:
EINVALβ Theenvnameargument points to an empty string or points to a string containing an'='character. empty string? an'='? absolutely not, we're not doing that today πENOMEMβ Insufficient memory was available to add a variable or its value to the environment. no memory, no vibes, no variable.
The following sections are informative.
π EXAMPLES π
None.
(yeah. literally none. the icon left the chat π)
π§ APPLICATION USAGE π§
See exec() for restrictions on changing the environment in multi-threaded applications.
β RATIONALE β
Unanticipated results may occur if setenv() changes the external variable environ. In particular, if the optional envp argument to main() is present, it is not changed, and thus may point to an obsolete copy of the environment (as may any other copy of environ). However, other than the aforementioned restriction, the standard developers intended that the traditional method of walking through the environment by way of the environ pointer must be supported.
It was decided that setenv() should be required by this version because it addresses a piece of missing functionality, and does not impose a significant burden on the implementor.
There was considerable debate as to whether the System V putenv() function or the BSD setenv() function should be required as a mandatory function. The setenv() function was chosen because it permitted the implementation of the unsetenv() function to delete environmental variables, without specifying an additional interface. The putenv() function is available as part of the XSI option.
The standard developers considered requiring that setenv() indicate an error when a call to it would result in exceeding {ARG_MAX}. The requirement was rejected since the condition might be temporary, with the application eventually reducing the environment size. The ultimate success or failure depends on the size at the time of a call to exec, which returns an indication of this error condition.
See also the RATIONALE section in getenv().
πΈ FUTURE DIRECTIONS πΈ
None.
π₯ SEE ALSO π₯
exec, getenv(), putenv(), unsetenv()
The Base Definitions volume of POSIX.1β2017, <stdlib.h>, <sys_types.h>, <unistd.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π«Ά brought to you by IEEE/The Open Group, dated 2017, and filed under SETENV(3POSIX) β go set some environment variables, but like, respectfully πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms