Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What limits the size of a command line, and which patterns avoid hitting it?

AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer

The kernel limits the combined size of arguments and environment for execve() (getconf ARG_MAX, often 2 MB, with a per-argument limit of 128 KB). rm $(find . -name '*.tmp') or rm *.log in a directory with 500,000 files fails with Argument list too long.

find /var/tmp/app -name '*.tmp' -delete                 # no argv at all
find /var/tmp/app -name '*.tmp' -exec rm -- {} +         # batches arguments
find /var/tmp/app -name '*.tmp' -print0 | xargs -0 rm -- # batches arguments

Builtins like printf '%s\0' * are not subject to the limit, because no exec happens.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells