The same question already asked here
Lua: Executing a string and storing the output of the command in a variable
But I want the function loadstring somehow return the result in string form that may be assigned to a string variable later to use further, what it returns is a function. Below code is an example:
ret = assert(loadstring(str_cmd))()
ret is a function:(
How can I get the REPL/loadstring output in string form?
If all output from str_cmd
comes from calls to print
, redefine print
before loading str_cmd
to save all output in a table. After running str_cmd
, use the table in table.concat
for instance.
Instead of redefining print
you can provide an environment to run str_cmd
in. You'll probably need to make that environment inherit from the global environment.