exec

The exec module runs external commands — either through a shell or directly with an argument list — captures their combined stdout/stderr, reads exit codes, and provides quick access to environment variables and the working directory. Import it with import exec.

Functions

FunctionSignatureDescription
runexec.run(cmd: string) → stringrun cmd via sh -c and return combined stdout/stderr
runArgsexec.runArgs(cmd: string, args: string[]) → stringrun a program directly (no shell) with args, returning combined output
exitCodeexec.exitCode(cmd: string) → intrun cmd via shell and return its exit code (-1 if it could not run)
envexec.env(key: string) → stringread an environment variable
setEnvexec.setEnv(key: string, val: string) → boolset an environment variable
cwdexec.cwd() → stringcurrent working directory
existsexec.exists(cmd: string) → booltrue if cmd is found on PATH

Example

import exec

if exec.exists("git") {
    let out = exec.run("git rev-parse --short HEAD")
    print(out)
}

// run a program directly with arguments, no shell parsing
print(exec.runArgs("echo", ["hello", "world"]))

// check whether a command succeeded
if exec.exitCode("test -f config.toml") == 0 {
    print("config present")
}
Standard library · View as Markdown · llms-full.txt