Bourne | Ash |  #!  | find | ARG_MAX | Shells | portability | permissions | UUOC | ancient | - | ../Various | HOME
"$@" | echo/printf | set -e | test | tty defs | tty chars | $() vs ) | IFS | using siginfo | nanosleep | line charset | locale


ARG_MAX, maximum length of arguments for a new process

or why do you get
command: arg list too long 

2009-06-03 (see recent changes)

Here you'll find


More about the nature of this limit

You will see this error message, if you tried to call a program with too many arguments, that is,
most likely in connection with pattern matching:

    $ command * 

On some systems the limit is even hit with "grep pattern /usr/include/* /usr/include/*/*". (You should not be doing this but using find.)

It's only the exec() system call (and its direct variants) which return the corresponding error condition E2BIG (<sys/errno.h>).
The shell is not to blame, it just delivers this error to you.
Shell expansion is not a problem, because here exec() is not needed, yet.
It's only limited by the virtual memory system resources [1].
Thus the following commands work smoothly, because they only make use of a shell built-in (echo) or a control structure (for loop):

    /dir-with-many-files$ echo * | wc -c
    /dir-with-many-files$ for i in * ; do grep ARG_MAX "$i"; done

There are different ways to learn the upper limit

(However, on the few system that have no limit for ARG_MAX, these methods wrongly might print a limit.)

From Version 7 on the limit was defined by NCARGS (usually in <[sys/]params.h>),
Later, ARG_MAX was introduced with 4.4BSD and System V.

In contrast to the headers, sysconf and getconf tell the limit which is actually in effect.
This is relevant on systems which allow to change it at run time (AIX), by reconfiguration (UnixWare, IRIX),
by recompiling (e.g. Linux) or by applying patches (HP-UX 10) - see the end notes for more details.
(Usually these are solutions for special requirements only, because increasing the limit doesn't solve the problem.)

    [1] However, in contrast to such expansions (which includes the literal overall command line length in scripts),
shells do have a limit for the interactive command line length (that is, what you may type in after the prompt).
But this limit is shell specific and not related to ARG_MAX.
Interestingly, putenv(3) is only limited by system resources, too. You just can't exec() anmymore if you are over the limit.
    [2] 4.4BSD BSD and the successors ( NetBSD since 1.0, OpenBSD 2.0, FreeBSD 2.0 ) provide: sysctl kern.argmax.
getconf in turn was introduced on BSDs with these versions: NetBSD 1.0, OpenBSD 2.0, FreeBSD 4.8.
    [3] A handy way to find the limits in your headers, if you have cpp(1) installed,
(inspired by Era Eriksson's page about ARG_MAX):

cpp <<EOF
#include <limits.h>
#include <params.h>
#include <sys/limits.h>
#include <sys/params.h>
arg_max: ARG_MAX
ncargs: NCARGS
EOF

The effectively usable space

When looking at ARG_MAX/NCARGS, you have to consider the space comsumption by both argv[] and envp[] (arguments and environment).
Thus you have to decrease ARG_MAX at least by the results of "env|wc -c" and "env|wc -l * 4" [3] for a good estimation of the currently available space.

    [3]  Every entry in envp is terminated with a null byte. The env utility cannot print it, but it adds a terminating newline instead, so the result of "wc -c" is the same.
"wc -l" accounts for the number of pointers in envp - usually 4 bytes each, according to sizeof().

POSIX suggests to subtract 2048 additionally so that the process may savely modify its environment. A quick estimation with the getconf command:
(formula suggested by Gunnar Ritter in <3B70A6AD.3L8115910@bigfoot.de>, de.comp.os.unix.shell)

 expr `getconf ARG_MAX` - `env|wc -c` - `env|wc -l` \* 4 - 2048

...and a way to determine it reliably

The most reliable way to get the currently available space is to test the success of an exec() with increasing length of arguments until it fails.
This might be expensive, but at least you need to check only once and the length of envp[] is considered automatically.

alternatively: about the GNU autoconf check

There's a autoconf check "Checking for maximum length of command line arguments...". It works quite similar.
However, it results in a much lower value (it can be 1/4 of the actual value) both by intention and for reasons of simplicity:

In a loop with increasing n, the check tries an exec() with an argument length of 2n.
The maximum is ARG_MAX/2 if ARG_MAX is a power of 2.
Finally, the found value is divided by 2 for reasons of safety. The reasoning is:
"C++ compilers can tack on massive amounts of additional arguments".

How to avoid the limit in a shell

If command * fails, then you can

Number of arguments (and maximum length of one argument)

At least on Linux 2.6, there's also a limit on the maximum number of arguments in argv[].
On Linux 2.6.14 the function do_execve() in fs/exec.c tests if the number exceeds
  PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *) / sizeof(void *)
On a 32-bit Linux, this is ARGMAX/4-1 (32767). This becomes relevant if the average length of arguments is smaller than 4.

Since Linux 2.6.23, this function tests if the number exceeds MAX_ARG_STRINGS in <linux/binfmts.h> (2^32-1 = 4294967296-1).
And as additional limit, one argument must not be longer than MAX_ARG_STRLEN (131072).


Actual values for ARG_MAX

The maximum length of arguments for a new process is varying so much among unix flavours, that i had a look at some systems:

System value getconf
available
default value determined by
non-competitive: Version 4 and 6 512 documentation of exec(2) in V4, V6 (but not yet in V3)
POSIX/SUSv2,v3 [posix] 4096 (minimum) + minimum _POSIX_ARG_MAX in <limits.h> , ARG_MAX
Version 7,
3 BSD,
System III,
Ultrix 3.1
5120 NCARGS in <sys/param.h>
SCO UNIX SysV R3.2 V4.0/4.2
SCO Open Desktop R2.0/3.0
5120 ? online documentation
4.1/4.2 BSD 10240 NCARGS in <sys/param.h>
4.3* BSD 20480 NCARGS in <sys/syslimits.h>
4.4 BSD alpha 20480 ? ARG_MAX in <sys/syslimits.h> (NCARGS in <sys/param.h>)
SunOS 3.x 10240 ? ARG_MAX in <sys/param.h>
MUNIX 3.2 10240 ? ARG_MAX in <sys/syslimits.h>
SINIX V5.2 10240 ? ARG_MAX in <limits.h>
UnixWare 7,
OpenUnix 8
10240 [uw/ou] ? ARG_MAX in <sys/limits.h>
Minix 3.1.1 16384 ARG_MAX in <limits.h>
EP/IX 2.2.1AA: 20480 ? ARG_MAX in <sys/limits.h>
HP-UX 8(.07), 9, 10 20478 + ARG_MAX in <limits.h>
IRIX 5.x, 6.x 20480 [irix] + (fallback: ARG_MAX in <limits.h>: 5120)
AIX 3.x, 4.x, 5.1[aix5] 24576 + ARG_MAX in <sys/limits.h> (NCARGS in <sys/param.h>)
AIX 6.1 1048576 + online documentation (ARG_MAX in <limits.h>)
SCO OpenServer 6.0.0 32768 + (fallback: ARG_MAX in <limits.h>: 10240)
OSF1/V4, V5 38912 + ARG_MAX in <sys/syslimits.h>
Unicos 9,
Unicos/mk 2
49999 + ARG_MAX in <sys/param.h>
Linux -2.6.22 131072 + ARG_MAX in <linux/limits.h> [linux-pre-2.6.23]
Linux 2.6.23 unlimited + kernel code [linux-2.6.23]
FreeBSD 2.0-5.5 65536 + ARG_MAX(/NCARGS) in <sys/syslimits.h> [freebsd]
FreeBSD 6.0 (powerpc 6.2, arm 6.3) 262144 + ARG_MAX(/NCARGS) in <sys/syslimits.h> [freebsd]
BSD/OS 4.1,
NetBSD 1.0+x,
OpenBSD x:
262144 + ARG_MAX(/NCARGS) in <sys/syslimits.h>
SunOS 5.x (32bit process) 1048320 [sunos5] + ARG_MAX in <limits.h> (NCARGS in <sys/param.h>)
SunOS 5.7+ (64bit process) 2096640 [sunos5] + ARG_MAX in <limits.h> (NCARGS in <sys/param.h>)
SCO OpenServer 5.0.6 1048576 + (fallback: ARG_MAX in <limits.h>: 5120)
SunOS 4.1.4 1048576 NCARGS in <sys/param.h> , sysconf(_SC_ARG_MAX)
HP-UX 11.00 2048000 [hpux] + ARG_MAX in <limits.h>
GNU Hurd / GNU libc   unlimited [hurd] ?  

[posix] See the online documentation (register for access) for getconf and <limits.h>.

[uw/ou] The limit on UnixWare can be increased by changing the kernel parameter ARG_MAX with /etc/conf/bin/idtune, (probably in the range up to 1MB) regenerating the kernel with "etc/conf/bin/idbuild -B" and rebooting. See also the online documentation.

[irix] The limit on IRIX can be changed by changing the kernel parameter ncargs with systune (in the range defined in /var/sysgen/mtune/kernel, probably varying from 64KB to 256KB), regenerating the kernel with "autoconfig" and rebooting. See also the online documentation of systune(1M) and intro(2).
[aix5] The limit on AIX 5.1 can be changed at run time with "chdev -l sys0 -a ncargs=value", in the range from 6*4KB to 1024*4KB. See also the online documentation for chdev (AIX documentation, Commands reference).
[freebsd] Interesting and everything but academic was the reason for the first of two increases (40960, 65536) on FreeBSD:
    "Increase ARG_MAX so that `make clean' in src/lib/libc works again.
    (Adding YP pushed it over the limit.)"
quoted from http://www.FreeBSD.org/cgi/cvsweb.cgi/src/sys/sys/syslimits.h
[linux-pre-2.6.23] On Linux, the maximum always has been PAGE_SIZE*MAX_ARG_PAGES (4096*32) minus 4.
However, in Linux-0.0.1, ARG_MAX was not known yet, E2BIG not used yet and exec() returned -1 instead.
With linux-0.10 it returned ENOMEM and with Linux-0.99.8 it returned E2BIG.
ARG_MAX was introduced with linux-0.96, but it's not used in the kernel code itself.
See do_execve() in fs/exec.c on http://www.oldlinux.org/Linux.old/.
[linux-2.6.23] With Linux 2.6.23, ARG_MAX can be unlimited; see the git entry.
For that purpose, the stack size must be set accordingly,
because ARG_MAX is still limited to 1/4-th of the stack size (ulimit -s).
This ensures that the program still can run at all. See the git diff of fs/exec.c

getconf ARG_MAX might still report the former limit (being careful about applications or glibc not catching up, but especially because the kernel <limits.h> still defines it)

[sunos5] On SunOS 5.5, according to <limits.h>, ARG_MAX is 1M, decreased by the following amount:
  "((sizeof(struct arg_hunk *))*(0x10000/(sizeof)(struct arg_hunk)))
   space for other stuff on initial stack like aux vectors, saved
   registers, etc.."  
On SunOS 5.9 this reads
  "ARG_MAX is calculated as follows:
   NCARGS - space for other stuff on initial stack
   like aux vectors, saved registers, etc.."
and <sys/params.h> defines NCARGS32/64 to 0x100000/0x200000 with NCARGS being substited at compile time. ARG_MAX is not calculated in the header files but is set directly in <limits.h>, also substitued at compile time from _ARG_MAX32/64.

SunOS 5.7 is the first release really supporting 64bit processes.

[hpux] HP-UX 11 can also run programs compiled on HP-UX 10. Programs which have ARG_MAX compiled in as buffer length and and copy from argv[]/envp[] without boundary checking might crash due to the increased ARG_MAX. See devresource.hp.com

[hurd] NCARGS in contrast is arbitrarily set to INT_MAX (2147483647)


Thanks to Gunnar Ritter for the test results from UnixWare and OpenUnix.
Thanks to Rodolfo Martín for access to OpenServer 6.

<http://www.in-ulm.de/~mascheck/various/argmax/>