exec
result = exec(program, [args], [shell=], [cwd=], [stdin=], [env=], [timeout=])
Input: program, args, shell, cwd, stdin, env, timeout · Output: result
What it is
Runs program (a Str) and waits, capturing what it printed. args is an optional List of arguments passed to the OS as separate items — not joined into a command line and re-split — so a path with a space in it needs no quoting rule. shell=true instead reads program as a whole command line for cmd /C (Windows) or sh -c, so pipes and && work. cwd= (a Str) runs it in that directory without moving the caller. stdin= (a Str) is written to the program's input, which is then closed, so a program reading to EOF gets exactly that and does not hang.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = exec(program, [args], [shell=], [cwd=], [stdin=], [env=], [timeout=]) | program StrRuns program (a Str) and waits, capturing what it printed.args List, optionalargs is an optional List of arguments passed to the OS as separate items — not joined into a command line and re-split — so a path with a space in it needs no quoting rule.shell optionalshell=true instead reads program as a whole command line for cmd /C (Windows) or sh -c, so pipes and && work.cwd Str, optionalcwd= (a Str) runs it in that directory without moving the caller.stdin Str, optionalstdin= (a Str) is written to the program's input, which is then closed, so a program reading to EOF gets exactly that and does not hang.env Record, optionalenv= (a Record of NAME = "value" pairs, written {NAME = "value"}) adds environment variables for the child only, e.g.timeout Record, optionaltimeout= (positive seconds) kills the child and its whole process tree when it runs longer: the Record then has timed_out = true, and success/exit_code report the killed process, not a lie. | stdout Stra Strstderr Stra Strsuccess —a Boolexit_code Numa Numenv= Recorda Record of NAME = "value" pairs, written {NAME = "value"}timeout= —positive seconds | Returns a Record with stdout (a Str), stderr (a Str), success (a Bool) and exit_code (a Num) — the same fields python_exec/js_exec/matlab_exec give back, plus timed_out. env= (a Record of NAME = "value" pairs, written {NAME = "value"}) adds environment variables for the child only, e.g. env = {main_memory = "12000000"} for pdflatex. timeout= (positive seconds) kills the child and its whole process tree when it runs longer: the Record then has timed_out = true, and success/exit_code report the killed process, not a lie. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
r = exec("echo hello from a program", shell = true)
print("said: {trim(r.stdout)}")
print("success {r.success}, exit code {r.exit_code}")
failed = exec("exit 3", shell = true)
print("a failure is data, not an error: success {failed.success}, code {failed.exit_code}")
said: hello from a program success true, exit code 0 a failure is data, not an error: success false, code 3
from file-io
How it works
module's own doc comment for why running a program and launching one are two builtins rather than one. Both are on check_sandbox's deny list, and the single guard at the top of this function is what applies it. Calling it again HERE would deny them always, sandboxed or not -- that guard is the only place self.sandboxed is read.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.