What is the maximum size of an environment variable value?

Is there a limit on the amount of data that can be stored in an environment variable on Linux, and if so: what is it?

On Windows, I found the following article an article in KB that summarizes: Windows XP or later: 8191 characters Windows 2000 / NT 4.0 characters: 2047

+64
linux shell environment-variables
Jul 03 '09 at 6:45
source share
7 answers

I do not think that for Linux there is a restriction on the environment variable. The total size of all environment variables collected together is limited during execve (). For more details see "Limitations on the size of arguments and environment" here .

A process can use setenv () or putenv () to extend the environment beyond the original space allocated by exec.

Here is 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; } 
+63
Jul 03 '09 at 7:10
source share

Well, this is at least 4M on my box. At that moment I became bored and wandered. I hope the terminal’s exit will be completed until I get back to work on Monday :-)

 export b1=A export b2=$b1$b1 export b4=$b2$b2 export b8=$b4$b4 export b16=$b8$b8 export b32=$b16$b16 export b64=$b32$b32 export b128=$b64$b64 export b256=$b128$b128 export b512=$b256$b256 export b1k=$b512$b512 export b2k=$b1k$b1k export b4k=$b2k$b2k export b8k=$b4k$b4k export b16k=$b8k$b8k export b32k=$b16k$b16k export b64k=$b32k$b32k export b128k=$b64k$b64k export b256k=$b128k$b128k export b512k=$b256k$b256k export b1m=$b512k$b512k export b2m=$b1m$b1m export b4m=$b2m$b2m echo $b4m AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA : : : : : : : : : : : : AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

If you are concerned that 4M might not be enough for your environment variable, you can rethink how you do it.

It might be better to put the information in a file, and then use an environment variable to reference that file. I have seen cases when a variable has the form @/path/to/any/fspec , it gets the actual information from the file path/to/any/fspec . If it does not start with @ , it uses the value of the environment variable itself.




Interestingly, given all these variables, each individual team begins to complain that the argument list is too long, so even if it allows you to set them, it may not be able to run programs after you have done this (since it should transfer the environment to these programs).

+20
Jul 03 '09 at 7:38
source share

I did a quick test on my Linux machine with the following snippet:

 a="1" while true do a=$a$a echo "$(date) $(numfmt --to=iec-i --suffix=B --padding=7 ${#a})" done 

On my box (Gentoo 3.17.8-gentoo-r1) this leads to (last lines of output):

 Wed Jan 3 12:16:10 CET 2018 16MiB Wed Jan 3 12:16:11 CET 2018 32MiB Wed Jan 3 12:16:12 CET 2018 64MiB Wed Jan 3 12:16:15 CET 2018 128MiB Wed Jan 3 12:16:21 CET 2018 256MiB Wed Jan 3 12:16:33 CET 2018 512MiB xrealloc: cannot allocate 18446744071562068096 bytes 

So: the limit is pretty high!

+5
Jan 03 '18 at 11:31
source share

I don’t know for sure, but a quick experiment shows that errors do not occur, for example. with 64 KB values:

 % perl -e 'print "#include <stdlib.h>\nint main() { return setenv(\"FOO\", \"", "x"x65536, "\", 1); }\n";'\ | gcc -xc -o envtest - && ./envtest && echo $? 0 
+1
Jul 03 '09 at 7:11
source share

I used this very fast and dirty php code (below), changing it for different values ​​and found that it works for variable lengths up to 128k. After that, for some reason this will not work; no exception occurs, no error is reported, but the value is not displayed in the subshell.

Maybe this is php restriction? Perhaps there are php.ini settings that can affect it? Or maybe there is a limit on the size of the vars that the subshell inherits? There may be appropriate kernel or shell configuration settings.

In any case, by default, on CentOS, the limit for installing var in the environment through putenv in php is apparently around 128k.

 <?php $s = 'abcdefghijklmnop'; $s2 = ""; for ($i = 0; $i < 8100; $i++) $s2 .= $s; $result = putenv('FOO='.$s2); print shell_exec('echo \'FOO: \'${FOO}'); print "length of s2: ".strlen($s2)."\n"; print "result = $result\n"; ?> 

Version Information -

 [root@localhost scratch]# php --version PHP 5.2.6 (cli) (built: Dec 2 2008 16:32:08) <..snip..> [root@localhost scratch]# uname -a Linux localhost.localdomain 2.6.18-128.2.1.el5 #1 SMP Tue Jul 14 06:36:37 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux [root@localhost scratch]# cat /etc/redhat-release CentOS release 5.3 (Final) 
+1
Aug 05 '10 at 20:55
source share

The command line (with all the argument) plus the environment variable should be less than 128 thousand.

0
Jul 03 '09 at 7:14
source share

very useful getconf -a |grep MAX or xargs --show-limits

0
May 08 '19 at 17:57
source share



All Articles