Environment variables are a legacy mess: Let's dive deep into them
5 hours ago
- #programming
- #linux
- #environment-variables
- Environment variables are a legacy interface in software development, lacking namespaces and types.
- Environment variables are passed from parent to child processes via the `execve` syscall in Linux.
- The kernel dumps environment variables on the stack as null-terminated strings during program launch.
- Bash stores environment variables in a stack of hashmaps, allowing local scopes and exports.
- Glibc manages environment variables via a dynamic `environ` array with linear time complexity for operations.
- Python's `os.environ` is built from the C library's `environ` array, with one-directional updates.
- Linux imposes size limits on environment variables: 128 KiB per variable and 2 MiB total.
- POSIX allows lowercase environment variables for applications, but uppercase is the convention.
- Environment variable names should avoid '=' and use `^[A-Z_][A-Z0-9_]*$` for portability.
- Values should use UTF-8 or the POSIX Portable Character Set for maximum compatibility.