How long can the command line be?

This page attempts to explain the meaning and the consequences of the ARG_MAX kernel constant. It's more or less related to my Useless Use of X Award page and a recurring topic in comp.unix.shell

Contents

The ARG_MAX constant

The value of the constant ARG_MAX defines how long an argument list your kernel can take on the command line before it chokes. Here's a script to find out the value of it:
	# Print out the system's architecture
	uname -rms              # or uname -a

	# Let the C preprocessor read in the system's limits from limits.h 
	# and then expand the macro ARG_MAX
	# Filter out excess output (there will be some line number information
	# and a gadzillion empty lines)
	#
	# On some gcc systems, I have been unable to find a working cpp.
	# You can use gcc -E - <:<:HERE | tail -1 in that case.

	cpp <<HERE | tail -1    # Only the last line is interesting
	#include <limits.h>
	ARG_MAX
	HERE
And here are some results from various machines I happen to have access to: POSIX requires ARG_MAX to be at least 4096 bytes. There are allegedly old systems with ARG_MAX set as low as 2048 or even 1024 bytes.

Consequences

On a system with a low value for ARG_MAX, you will often end up with the shell saying Too many arguments or Argument list too long when you attempt something like
	ls * */*
or even
	ls *
and of course
	for f in `find . -print`; do
		: something
	done
On any system, the last of the examples is going to blow up sooner or later. This is why we have xargs:
	find . -print |
	xargs : something
This is of course still not a secure construction (depending of course on what the : something attempts to do). But that's another story.

Further reading

W. Richard Stevens, Advanced Programming in the Unix Environment, p30f, p39; p209
Addison-Wesley, 1992; ISBN 0-201-56317-7

Back to Unix Award Page