I've been seeing a strange behavior when messing up with the environment variables. I'm setting up a very long environment variable and this prevent launching any command:

( Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-66-generic x86_64) )

$ export A=$(python -c "print 'a'*10000000")
$ env
-bash: /usr/bin/env: Argument list too long
$ ls
-bash: /bin/ls: Argument list too long
$ cat .bashrc
-bash: /bin/cat: Argument list too long
$ id
-bash: /usr/bin/id: Argument list too long

What is happening here ?


The list of arguments and the environment of a command are copied in the same space in memory when a program starts. The error message is “Argument list too long”, but in fact the exact error is that the argument list plus the environment is too long.

This happens as part of the execve system call. Most if not all unix variants have a limit on the size of this temporary space. The reason for this limit is to avoid a buggy or malicious program causing the kernel to use a huge amount of memory outside of that program's own memory space.

The POSIX standard specifies that the maximum size of this memory space must be at least ARG_MAX, and that the minimum value of that (_POSIX_ARG_MAX) is 4096. In practice most Unix variants allow more than that, but not 10MB. You can check the value on your system with getconf ARG_MAX. On modern Linux systems, the maximum is 2MB (with typical settings). Traditionally many systems had a 128kB limit. Linux also still has a 128kB limit for the value of a single argument or the definition of an environment variable.

If you need to pass more than a few hundred bytes of information, pass it in a file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.