Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum size of a Linux 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

like image 488
Gio Avatar asked Jul 03 '09 06:07

Gio


People also ask

How big can a Linux environment variable be?

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.

What is the maximum size of a variable?

A variable can hold up to 10240 characters.

How large can a bash variable be?

It's 128KB, like the former ARG_MAX.

What are 3 types of environment variables in Linux shell?

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.


1 Answers

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; } 
like image 177
sigjuice Avatar answered Sep 22 '22 02:09

sigjuice