0

I am trying to set an environment variable the value of which happens to be too long. As soon as I am done with the export I am getting this error for all the commands.

-bash: /usr/bin/ls: Argument list too long

Is there a way I can still set this env variable or if I can increase the size of ARG_MAX

I am using 5.4.0-139-generic kernel version and Ubuntu 20.04

tushar_ecmc
  • 186
  • 2
  • 21
  • I don't think your problem is a limit with env. vars. The problem is more that you're asking too much of `ls` - likely you have a wildcard like `ls *` in your script and that glob (`*`) is expanding to a very large number of files which leads to `ls` complaining with the above. See https://stackoverflow.com/questions/11289551/argument-list-too-long-error-for-rm-cp-mv-commands – Gedge May 18 '23 at 09:32
  • I am only executing `ls -al` – tushar_ecmc May 19 '23 at 05:09

1 Answers1

1

This is a FAQ: https://stackoverflow.com/search?q=Argument+list+too+long. TL;DR: The problem is not the length of any individual env var. The problem is the total length of all env vars and CLI arguments passed to a program. The error is because the execve syscall (used to run a program) is returning error E2BIG (man execve). Depending on the OS you may be able to increase the limit. But really, if you have an env var that is so large it is causing this problem you should be storing the data someplace other than an env var. Perhaps in a file whose name is the value of an env var.

Kurtis Rader
  • 6,734
  • 13
  • 20
  • I don't have other option apart from keeping everything in env var. Can you suggest me the way how can I increase this limit as I am using ubuntu 20.04 – tushar_ecmc May 19 '23 at 10:10
  • 1
    See https://superuser.com/questions/705928/compile-the-kernel-to-increase-the-command-line-max-length for one decent explanation. The short answer is on modern Linux distros the arg max limit is 1/4 of the stack size. The default stack size is 8 MB so the default arg max is 2 MB. Use the `ulimit` command to increase the stack size. P.S., I find it hard to believe you truly have no option but to put so much data in a single env var. – Kurtis Rader May 19 '23 at 19:07