exec
result = exec(program, [args], [shell=], [cwd=], [stdin=])
Input: program, args, shell, cwd, stdin · 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=]) | 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. | stdout Stra Strstderr Stra Strsuccess —a Boolexit_code Numa Num | Returns a Record with stdout (a Str), stderr (a Str), success (a Bool) and exit_code (a Num) — the same four fields python_exec/js_exec/matlab_exec already give back. |
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.