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.
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.
The shell/OS imposed limit is 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 and 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.
ARG_MAX indeed limits the total size of the command line and the environment, but you're facing an additional limitation: one argument must not be longer than MAX_ARG_STRLEN (which is unfortunately hard-coded to be 131072).
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
xargs
provides another method to get detailed information on command line length limit, including the theoretical maximum for your system, the usable maximum, the minimum specified by POSIX and the current buffer size limit.
You do this by providing no input to xargs
and providing the option --show-limits
. The man page also recommends providing the --no-run-if-empty
option (a GNU extension).
Example Use/Output
$ xargs --no-run-if-empty --show-limits </dev/null
Your environment variables take up 3643 bytes
POSIX upper limit on argument length (this system): 2091461
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087818
Size of command buffer we are actually using: 131072
Maximum parallelism (--max-procs must be no greater): 2147483647