getenv(3)

๐Ÿ‘‘ getenv(3) โ€” Library Functions Manual ๐Ÿ‘‘

okay bestie, say hi to getenv(3) โ€” this is the libc call that goes digging through your process's environment list to pull out an environment variable for you. And it brought a security-conscious bestie along too: secure_getenv. Both of them live in the Standard C library (libc, -lc). no cap ๐Ÿ’…


โœจ NAME โœจ

getenv, secure_getenv โ€” get an environment variable ๐ŸŒธ


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc)


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <stdlib.h>

char *getenv(const char *name);
char *secure_getenv(const char *name);

๐Ÿ’œ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

  • secure_getenv(): _GNU_SOURCE

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

so getenv() literally scrolls the environment list looking for the variable called name, and hands you back a pointer to the matching value string. it's giving personal assistant energy ๐Ÿซถ

secure_getenv() is exactly the same as getenv() except for one icon behaviour: it returns NULL in any case where "secure execution" is required. dramatic? yes. necessary? also yes ๐Ÿ”ฅ

Secure execution is required if any one of these was true when the program run by the calling process was loaded:

  • the process's effective user ID did not match its real user ID, or the process's effective group ID did not match its real group ID (typically this is the result of executing a set-user-ID or set-group-ID program);
  • the effective capability bit was set on the executable file; or
  • the process has a nonempty permitted capability set.

Secure execution may also be required if it got triggered by some Linux security modules. ๐Ÿ‘€

The whole point of secure_getenv() is to live in general-purpose libraries so you avoid the vulnerabilities that happen when set-user-ID or set-group-ID programs accidentally trust the environment. like, imagine trusting the environment. couldn't be her ๐Ÿ™ˆ


๐Ÿ’– RETURN VALUE ๐Ÿ’–

getenv() returns a pointer to the value in the environment โ€” or NULL if there's no match. that's it, that's the whole return policy bestie ๐Ÿ’…


๐Ÿงš ATTRIBUTES ๐Ÿงš

for an explanation of the terms used in this section, see attributes(7). (no tables here, we're keeping it cute and listy โญ)

  • Interface: getenv(), secure_getenv()
  • Attribute: Thread safety
  • Value: MT-Safe env

โญ STANDARDS โญ

  • getenv() โ€” C11, POSIX.1-2008.
  • secure_getenv() โ€” POSIX.1-2024.

๐Ÿ“œ HISTORY ๐Ÿ“œ

  • getenv() โ€” POSIX.1-2001, C89, C99, SVr4, 4.3BSD.
  • secure_getenv() โ€” POSIX.1-2024, glibc 2.17.

look at her, she's been around. veteran queen energy ๐Ÿ‘‘


๐Ÿ’€ NOTES ๐Ÿ’€

The strings in the environment list are shaped like name=value. that's the whole format, no frills ๐Ÿ“

As typically implemented, getenv() returns a pointer to a string inside the environment list. so bestieโ€ฆ do not modify that string. like, at all. modifying it changes the environment of the process. it's not a little oopsie, it's a whole situation ๐Ÿ˜ญ

The implementation of getenv() is not required to be reentrant. the string your return value points to may be statically allocated, and it can get modified by a subsequent call to getenv(), putenv(3), setenv(3), or unsetenv(3). so if you're holding onto that pointer, protect your peace and copy it ๐Ÿซถ

โš ๏ธ warning bestie: that pointer is not yours forever. it's borrowed, and someone else's call can rewrite it out from under you.

The "secure execution" mode of secure_getenv() is controlled by the AT_SECURE flag contained in the auxiliary vector passed from the kernel to user space. iykyk ๐Ÿงš


๐ŸŒธ SEE ALSO ๐ŸŒธ

clearenv(3), getauxval(3), putenv(3), setenv(3), unsetenv(3), capabilities(7), environ(7)


that's the whole page, and it's iconic. this one's from Linux man-pages 6.20, dated 2026-02-08 โ€” go check your env, stay safe, and never trust the environment in a setuid program. periodt ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms