Gio Gio - 9 months ago 53
Bash Question

What is the maximum size of an environment variable value?

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

Answer Source

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) {
        return 1;

    memset(var, 'X', size);
    var[size - 1] = '\0';
    var[0] = 'A';
    var[1] = '=';

    if (putenv(var) != 0) {
        return 1;

/*  Demonstrate E2BIG failure explained by paxdiablo */
    execl("/bin/true", "true", (char *)NULL);

    printf("A=%s\n", getenv("A"));

    return 0;