Unix / Linux: Maximum Character Length of Arguments In a Shell Command

by on October 3, 2012 · 5 comments· LAST UPDATED October 3, 2012

in

What is the maximum character length of arguments in a shell command? How do I find out the maximum length of arguments for a new process under Linux or Unix like operating systems?

If you get an error that read as - command: Argument list too long due to limit for the command line length.

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion timeN/A
UNIX / Linux / BSD system has a limit on how many bytes can be used for the command line argument and environment variables. You need to use the getconf command to query system configuration variable called ARG_MAX. The following command will provide the upper limit for your system under Linux operating systems:
$ getconf ARG_MAX
Sample outputs:

2097152

The following bash code will provide you exact number:

 
echo $(( $(getconf ARG_MAX) - $(env | wc -c) ))
 

POSIX suggests to subtract 2048 additionally so that the process may savely modify its environment:

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

Sample outputs:

2092496

Please note that the maximum length of arguments for a new process may differ among unix flavours. I recommend that you go through this web page for detailed information.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 Filipe October 3, 2012 at 12:57 pm

I understand that you subtract 2048 to allow process to modify env variables, I just don’t get it why you subtract byte count from env command, and line count? Could you explain it please? Thanks!

Reply

2 Jalal Hajigholamali October 4, 2012 at 12:03 pm

Hi,

Please explain why you are using env| wc …

thanks

echo $(( $(getconf ARG_MAX) – $(env | wc -c) ))

Reply

3 nixCraft October 6, 2012 at 8:35 am

You need to get the effectively usable space. You need to consider the space consumption by both argv[] (arguments) and envp[] (environment). So you’ve to decrease ARG_MAX at least by the results of “env|wc -c” and “env|wc -l * 4″. POSIX suggests to subtract 2048 additionally so that the process may savely modify its environment. See linked article at the bottom of faq for more info.

Reply

4 Stef October 5, 2012 at 5:24 pm

My guess is that `env | wc -c` gives the size needed to store all environment variables (names and values). Envirnoment variables are not global. Each process has a fulk copy inherited from its parent process.

`env | wc -l ` * 4 gives the number of environment variables multiplied by 4. This is probably the size needed to allocate a table of pointers (or offsets or sizes) to speedup the accesses to the environment variables.

Reply

5 mary ann smith October 11, 2012 at 4:26 pm

how do i find java for my tablet

Reply

Leave a Comment

Tagged as: , , , , , , , , ,

Previous Faq:

Next Faq: