whenever someone asks me "what is the maximum variable length?" i always tell them they're asking the wrong question :P the length of a variable's value is ultimately constrained by a few other fundamental limits:

  • the length limit of a single command. this is imposed before and after evaluation of code, and you'll see this as "* /command: line too long". this is thrown when the command exceeds 4,151 characters.
  • the length limit of an identifier's return value. if an identifier tries to return more than 4,150 characters, you'll typically see "* Line too long: $ident"
  • a little more subtle but also relevant: the length limit of a argument to an identifier. you can pass a LOT of data to your custom identifiers if you so wish, //noop $myalias($str(a, 4096), $str(b, 4096), $str(c, 4096), $str(d, 4096)) is 32kb for example (each character occupying 2 bytes in UTF-16).

    it's unlikely that you've come across a piece of code that breaches this limit since the majority of length limit errors come from attempting to exceed the other 2, but you can see it with //noop $len( [ $str(a, 4150) b ] ) which produces "* Evaluation error: $len".

    normally, the arguments to identifiers are evaluated without error but truncated at the token (identifier, variable or word) that causes the value to exceed 4,151 characters. this is why //echo -a $len($str(a, 4000) $str(b, 4000)) produces '4000' rather than yielding an error.


putting these facts together, we can now attempt to create a variable of maximum permissible length by using $regsub() to implicitly assign to it:

Code:
//var %max | noop $regsub( , , $str(a, 4149) b, %max) | echo -a $len(%max) - $asc($mid(%max, -1))


due to some extra internal details that we can only speculate about, the length of %a turns out to be 4,150 instead of 4,151 (the 'b' gets truncated). anyway, this appears to be the maximum.


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde