0

While quite dealt upon with in the forums, the questions usually focus on environment variables on linux box.

I wonder if there is any specification, clear determination regarding maximum (limiting) size a regular variable (a='Hello') can hold.

Is the size limited by memory only?

Is there a fixed/flexible limit imposed on regular variables?

What is the maximum size I can load into a variable (a=$(cat bigfile.txt)) until strange things start to happen?

I would like to know where is the limit so I can decide whether or not it is a good idea to use variable to load text file into it. (and how big it could be to still feel within limits) and if the system gives clear error when the limit is reached.

1 Answers1

0

My experiment on the machine with 1GB RAM show interesting result. Script I use is:

TEMPVAR=A
for i in {1..1000};
        do
        TEMPVAR=${TEMPVAR}$TEMPVAR;
        a=$(echo 2^$i+1|bc)
        b=$(echo $TEMPVAR|wc -c)
        if [ $a -ne $b ]
        then echo KO - $a, $b
        else echo $a
        fi
done

And get those results:

3
5
<snip>
134217729
268435457
./z1.1.sh: xmalloc: cannot allocate 1073741825 bytes (0 bytes allocated)
KO - 536870913, 0
./z1.1.sh: xmalloc: cannot allocate 1073741825 bytes (0 bytes allocated)

So in general you can keep quite a large amount of information in variables. You can find more info in my personal blog

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
  • Thanks for the input. Is there any clasification/technical description on the variable size limits regarding linux (and how they are determined) or are only experiments available and there's no definition on how variables behave when it comes to size of its content? I would like to read the specification to understand how does it work, more than trying blindly when does it break. (Alternatively: knowing why does it break) – user12938074 Mar 09 '20 at 10:00
  • @user12938074, according to this answer https://stackoverflow.com/a/1078125/2908599 there is no defined limit for environment variables. But I found this answer: https://unix.stackexchange.com/a/521377/101265 where it give different value (much low then first answer and my test) – Romeo Ninov Mar 09 '20 at 10:05
  • Thank you, I still feel like it's orbiting mainly around environment variables, and I would like to know how simple variable works. From your experiment it seems it stops when the memory limit is reached. – user12938074 Mar 09 '20 at 10:10
  • @user12938074, actually quart of memory. When try to allocate 512MB it fail. And this is because of the way I double the size of variable. But at the end we talk about 256MB in one variable. Which is a lot :) – Romeo Ninov Mar 09 '20 at 10:15
  • 1
    it is a lot for sure, but limit might be different on different hardware or possibly due to some kernel settings and that's what 's keeping me awake. (no clear definition on maximum size a variable can hold). That there seems to be no clear definition e.g. "Variable can be maximally FREE_MEMORY * 0.5" in size etc. Being dependent on experiments may lead to errors in random circumstances. – user12938074 Mar 09 '20 at 10:19