53

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.

robdog
  • 633
  • 1
  • 5
  • 5
  • 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
    http://stackoverflow.com/questions/6846263/maximum-length-of-command-line-argument-that-can-be-passed-to-sqlplus-from-lin || http://askubuntu.com/questions/14081/what-is-the-maximum-length-of-command-line-arguments-in-gnome-terminal || http://serverfault.com/questions/163371/linux-command-line-character-limit || http://unix.stackexchange.com/questions/120642/what-defines-the-maximum-size-for-a-command-single-argument – Ciro Santilli OurBigBook.com Jun 22 '15 at 09:45
  • I can do `echo $(python -c "print('.' * 100000000)")` on Ubuntu 19.10. – Boris Verkhovskiy Mar 30 '20 at 19:39
  • 3
    @Boris That's because `echo` is a builtin. Try `/bin/echo $(python -c "print('.' * 100000000)")` and you will get something like `bash: /bin/echo: Argument list too long` – Tyilo Sep 29 '21 at 20:04

4 Answers4

58

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.

David Spillett
  • 22,754
  • 45
  • 67
  • 3
    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
  • 3
    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
  • Is the call itself (to the path of the script/binary) considered as/in an argument? This would shorten the argument length if the target (path) is deeply located. – Ben Aug 19 '21 at 13:06
  • Note: this solution is not limited to Linux, because `getconf` is actually [part of POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.html). For example, macOS also provides this command. – Franklin Yu Dec 16 '22 at 08:01
24

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).

See https://unix.stackexchange.com/questions/120642/what-defines-the-maximum-size-for-a-command-single-argument

Rotsor
  • 341
  • 2
  • 3
  • I think `MAX_ARG_STRLEN` has been based on current stack size limits since around 2011. Try `ulimit -S -s unlimited` before trying to execute a very long command line. Note that `sudo` will overwrite your limits so you cannot run very long `sudo ...` even after setting stack size. – Mikko Rantalainen Nov 02 '21 at 11:03
  • @MikkoRantalainen, I can still see `MAX_ARG_STRLEN` being a hard-coded constant in [latest Linux](https://github.com/torvalds/linux/blob/v5.16-rc7/include/uapi/linux/binfmts.h). Maybe you're thinking `ARG_MAX`? – Rotsor Jan 01 '22 at 05:10
  • Yeah, I meant `ARG_MAX` because it defines the command line and size of the environment and scales automatically according to current stack. The `MAX_ARG_STRLEN` is hardcoded as you wrote and doesn't usually cause problems. – Mikko Rantalainen Jan 02 '22 at 21:08
  • 1
    @MikkoRantalainen, except when it does, as is the case in this question. – Rotsor Jan 04 '22 at 20:16
4

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
gm3dmo
  • 10,057
  • 1
  • 42
  • 36
3

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