Are shell variables limited in size? And which is the max size a variable can hold?
Asked
Active
Viewed 1.5k times
7
-
2The answer would vary depending on what shell you are using, but if have to worry about the size of a shell variable, you probably should be using a more general purpose scripting language like Python or Ruby. – mikerobi Mar 15 '10 at 23:59
-
1+1 Good suggestion, I think I'm writing an AWK script for this. – helpermethod Mar 16 '10 at 00:03
1 Answers
8
Yes they can be. It depends on your OS and/or the shell flavours and versions. It is safer to use temporary files if you expect variable values to exceed 1-4kB.
EDIT
Also see What is the maximum size of an environment variable value?; this deals with the OS limitation on total environ
size (cumulative size of all VARIABLE=VALUE
s) which affects export
ed variables, but the shell itself may have its own limitations re. all (including non-export
ed) variable sizes.
This being said, unless you have portability in mind, GNU bash
is relatively good about not limiting (non-export
ed) variables' sizes and can very likely hold arbitrary amounts of data as long as malloc
can find sufficient memory and contiguous address space. :)