Argument List Length – How to Find the Maximum Argument List Length

argumentslimit

Following ARG_MAX, maximum length of arguments for a new process it seems like ARG_MAX is wrongly (or at least ambiguously) defined on my Mac Mini 3,1 running Ubuntu 12.04:

$ getconf ARG_MAX # arguments 
2097152
$ locate limits.h | xargs grep -ho 'ARG_MAX[ \t]\+[0-9]\+' | uniq | cut -d ' ' -f 8
131072

The actual limit seems to be somewhere between these:

$ cd "$(mktemp -d)"
$ touch $(seq 1 131072) && find . -mindepth 1 -printf x | wc -c && rm *
131072
$ touch $(seq 1 131073) && find . -mindepth 1 -printf x | wc -c && rm *
131073
$ touch $(seq 1 $(getconf ARG_MAX)) && find . -mindepth 1 -printf x | wc -c && rm *
bash: /usr/bin/touch: Argument list too long

I did a small search:

cd "$(mktemp -d)"
min=131072
max=2097152
while true
do
    search=$((min + (max - min) / 2))
    if touch $(seq 1 $search) 2>/dev/null
    then
        min=$search
    else
        max=$search
    fi
    [[ $((max - min)) -le 1 ]] && echo "ARG_MAX = $min" && break
done

Eventually this resulted in ARG_MAX = 314290, which doesn't seem to have any relation to either of the ARG_MAX values found before. Is this normal? Is there a simpler way to find the actual ARG_MAX?

Did I misunderstand the definition of ARG_MAX? It seems it's actually the byte (or possibly character) length of the arguments with or without (?) the separating spaces. If it's really the byte length, are there also other restrictions?

Best Answer

Yes, it's the length in bytes, including the environment.

Very roughly:

$ { seq 1 314290; env; } | wc -c
2091391

linux sysconf

The maximum length of the arguments to the exec(3) family of functions. Must not be less than _POSIX_ARG_MAX (4096).

POSIX 2004 limits.h

Maximum length of argument to the exec functions including environment data. Minimum Acceptable Value: {_POSIX_ARG_MAX}

Related Question