title: Exact limit to avoid "Argument list too long "
body: <p>I know that there are some tricks to avoid the shell's limit, which leads to "argument list too long", but I want to understand why the limit hits in my case (even though it should not). As far as I know the limit of chars in an argument for a command should be able to be determined by the following steps:</p>
  
  <ol>
  <li>Get the maximum argument size by <code>getconf ARG_MAX</code></li>
  <li>Subtract the content of you environment retrieved by <code>env|wc -c</code></li>
  </ol>
  
  <p>On my machine with Fedora 30 and zsh 5.7.1 this should allow me argument lists with a length of up to 2085763 chars. But I already hit the limit with only 1501000 chars. What did I miss in my calculation?</p>
  
  <hr>
  
  <p>Minimal working example for reproduction:</p>
  
  <p>Setting up files:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ for i in {10000..100000}; do touch Testfile_${i}".txt"; done
  $ ls Testfile*
  zsh: argument list too long: ls
  </code></pre>
  
  <p>No I deleted stepwise (1000 files per step) files to check when the argument line was short enough to be handled again</p>
  
  <pre class="lang-sh prettyprint-override"><code>for i in {10000..100000..1000}; do echo $(ls|wc -l); rm Testfile_{$i..$((i + 1000))}.txt; ls Testfile_*|wc -l; done
  </code></pre>
  
  <p>The message <code>zsh: argument list too long: ls</code> stops between 79000 and 78000 remaining files. Each filename has a length of 18 chars (19, including the separating whitespace), so in total at this moment the argument line should have a total length of 79000*19=1501000 respectively 78000*19=1482000 chars.</p>
  
  <p>This result is the same magnitude in comparison to the expected value of 2085763 chars but still it's slighty off. What could explain the difference of 500000 chars?</p>
  
  <hr>
  
  <h2>ADDENDUM1:</h2>
  
  <p>Like suggested in the comments I ran <code>xargs --show-limits</code> and the output fits round about my expectation.</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ xargs --show-limits
  Your environment variables take up 4783 bytes
  POSIX upper limit on argument length (this system): 2090321
  POSIX smallest allowable upper limit on argument length (all systems): 4096
  Maximum length of command we could actually use: 2085538
  Size of command buffer we are actually using: 131072
  Maximum parallelism (--max-procs must be no greater): 2147483647
  </code></pre>
  
  <h2>ADDENDUM2:</h2>
  
  <p>Following the comment of @Jens I now added 9 Bytes additional overhead to the words (8 Bytes for the pointer, 1 for the terminating NUL-Byte). Now I get the following results (I do not know, how the whitespace is handled, for the moment I leave it out):
  79000*(18+9)= 2133000
  78000*(18+9)= 2106000</p>
  
  <p>Both values are much closer to the theoretical limit than before...indeed, they are even a bit above it. So together with some safety margin I'm more confident to preestimate the maximal argument length.</p>
  
  <hr>
  
  <h2>Further reading:</h2>
  
  <p>There are more posts about this topic, of which none answers the question in a satisfying way, but still they provide good material:</p>
  
  <ul>
  <li><a href="https://unix.stackexchange.com/a/120842/211477">https://unix.stackexchange.com/a/120842/211477</a></li>
  <li><a href="https://www.in-ulm.de/~mascheck/various/argmax/" rel="nofollow noreferrer">https://www.in-ulm.de/~mascheck/various/argmax/</a></li>
  </ul>