Is there a limit to the amount of data that can be stored in an environment variable on Linux, and if so: what is it?
For Windows, I've found following KB article which summarizes to: Windows XP or later: 8191 characters Windows 2000/NT 4.0: 2047 characters
The theoretical maximum length of an environment variable is around 32,760 characters. However, you are unlikely to attain that theoretical maximum in practice. All environment variables must live together in a single environment block, which itself has a limit of 32767 characters.
A variable can hold up to 10240 characters.
It's 128KB, like the former ARG_MAX.
Below are some of the most common environment variables: USER - The current logged in user. HOME - The home directory of the current user. EDITOR - The default file editor to be used.
I don't think there is a per-environment variable limit on Linux. The total size of all the environment variables put together is limited at execve()
time. See "Limits on size of arguments and environment" here for more information.
A process may use setenv()
or putenv()
to grow the environment beyond the initial space allocated by exec.
Here's a quick and dirty program that creates a 256 MB environment variable.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { size_t size = 1 << 28; /* 256 MB */ char *var; var = malloc(size); if (var == NULL) { perror("malloc"); return 1; } memset(var, 'X', size); var[size - 1] = '\0'; var[0] = 'A'; var[1] = '='; if (putenv(var) != 0) { perror("putenv"); return 1; } /* Demonstrate E2BIG failure explained by paxdiablo */ execl("/bin/true", "true", (char *)NULL); perror("execl"); printf("A=%s\n", getenv("A")); return 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With