Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm passing a variable to a script on the command line. What is the character limit of a command? eg:

$ MyScript reallyreallyreally...reallyreallyreallylongoption

Thanks.

share|improve this question
1  
I do not understand the question. Are you interested in the limit of characters? –  krissi Jul 23 '10 at 12:25
    
You need to state your question more clearly, else its just going to get closed. –  ThatGraemeGuy Jul 23 '10 at 13:01

2 Answers 2

up vote 6 down vote accepted

The shell/OS imposed limit is generally very long - usually one or two hundred thousand characters.

getconf ARG_MAX will give you the maximum input limit for a command. On the Debian system I currently have a terminal open on this returns 131072 which is 128*1024. The limit is reduced by your environment variables as if my memory serves me correctly these are passed in the same structure by the shell, though that will only take off a few hundred characters in most cases. To find an approximation of this value run env | wc -c - this suggests 325 characters at the current time on this login on this machine.

Scripts are likely to permit this full length, but it is not unlikely that other utilities will impose their own limits either intentionally or through design issues. There may also be artificial limits to how long an individual argument on a long command line can be, and/or how long a path to a file can be.

share|improve this answer
    
On my system, getconf ARG_MAX gives 2097152, but the max arg length I can pass is still 131071 (and I don't have to deduct the size of the environment). –  Dennis Williamson Jul 23 '10 at 14:10
1  
Also remember that xargs and even find -exec are your friends when dealing with giant argument lists. –  coredump Jul 23 '10 at 14:15
    
@Dennis: the value returned by getconf is kernel level I think. Perhaps bash is setting a lower limit by its design/configuration? Also, my knowledge of this comes from some time ago so it could be that things have changed a little recently, though it isn't an area I'd expect to see a lot of movement in except in new experimental shells. –  David Spillett Jul 23 '10 at 19:07
    
I get the same results in ksh, zsh, dash, fish and Bash 3 as I did in Bash 4. The error message from fish may be informative: "fish: The total size of the argument and environment lists (130kB) exceeds the operating system limit of 2.0MB." However, set | wc -c is 306317 and env | wc -c is 2507 which don't account for the difference. I don't know what else is being counted. –  Dennis Williamson Jul 23 '10 at 22:02

Do you mean what is the longest variable length? To figure that out you can use perl's "x" to create a very long variable name:

 VAR=`perl -e 'print "a"x131071'` ; bash a.sh $VAR

On My system 131071 works:

and the variable is printed at 131072 it's too big:

VAR=`perl -e 'print "a"x131072'` ; bash a.sh $VAR
bash: /bin/bash: Argument list too long
share|improve this answer
1  
Actually that's a very long variable value. –  Dennis Williamson Jul 23 '10 at 14:02
1  
Also, you don't have to use perl and a script: /bin/echo "$(printf "%*s" 131071 ".")">/dev/null –  Dennis Williamson Jul 23 '10 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.