0

Grateful if anyone can please let me know what is the total length that a UNIX variable can take. I have a variable called filelist and I want to save the content like that :

filelist=path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name path/name.....infinity

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
user3905438
  • 441
  • 1
  • 4
  • 6

1 Answers1

2

There are two limitations. The first one is inside the shell. I guess that bash don't limit a lot its variables, the real limitation being available resources (if malloc fails).

The more important one (which is relevant for your scp $filelist id@remotohost:path case) is when you use that variable, e.g. to exec some program. Then execve(2) is called, and it could fail because the overall program arguments takes too much memory. Read the Limits on size of arguments and environment section of the man page:

Limits on size of arguments and environment

   Most UNIX implementations impose some limit on the total size of the
   command-line argument (argv) and environment (envp) strings that may
   be passed to a new program.  POSIX.1 allows an implementation to
   advertise this limit using the ARG_MAX constant (either defined in
   <limits.h> or available at run time using the call
   sysconf(_SC_ARG_MAX)).

In practice that execve limit is significant (e.g. 128Kbytes by default). I often compile my own kernel after editing some kernel header file to raise that limit (e.g. to 1Mbyte).

Notice that a variable which is export-ed in bash gets into the environment (of future forked processes) subject to above limitations.

BTW, you could transfer the files in some other ways (rsync or git etc...).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547