Skip to content
Docs
Modules
exec

exec

The exec module is used to run external commands.

Like the underlying Go os/exec package, this module does not invoke the system shell, expand glob patterns, or handle other shell features.

Callable

The exec module itself is callable, using one of two signatures. The preferred form is now:

Function signature
exec(args []string, opts map) result

The old form that is still supported for backwards compatibility is:

Function signature
exec(name string, args []string, opts map) result

This provides a shorthand way to build and run a command. The function returns a result object containing the stdout and stderr produced by running the command. The opts argument is optional.

Example
>>> exec(["echo", "TEST"]).stdout
byte_slice("TEST\n")

The opts argument may be a map containing any of the following keys:

NameTypeDescription
dirstringThe working directory of the command.
envmapThe environment given to the command.
stdinstring, byte_slice, or readerThe standard input given to the command.
stdoutwriterThe standard output destination.
stderrwriterThe standard error destination.

Functions

command

Two signatures are supported for the command function. The preferred form is now:

Function signature
command(args []string) command

The old form that is still supported for backwards compatibility is:

Function signature
command(name string, args ...string) command

Creates a new command with the given name and arguments. The command can then be executed with its run, start, output, or combined_output methods. Before the command is run, its path, dir, and env attributes may be set. Read more about the command type below.

Example
>>> exec.command(["echo", "TEST"]).output()
byte_slice("TEST\n")

look_path

Function signature
look_path(name string) string

Searches for the named executable in the directories contained in the PATH environment variable. If the name contains a slash, it is tried directly, without consulting the PATH. Otherwise, the result is the absolute path to the named executable.

Example
>>> exec.look_path("echo")
"/bin/echo"

Types

command

Represents an external command that is being built and run.

Attributes

NameTypeDescription
pathstringThe path to the executable.
dirstringThe working directory of the command.
env[]stringThe environment given to the command.
stdoutbyte_sliceThe standard output produced by the command.
stderrbyte_sliceThe standard error produced by the command.
runfunc()Runs the command and waits for it to complete.
outputfunc() byte_sliceRuns the command and returns its standard output.
combined_outputfunc() byte_sliceRuns the command and returns its combined standard output and standard error.
startfunc()Starts the command but does not wait for it to complete.
waitfunc()Waits for the command to exit.

Examples

Example
>>> c := exec.command("pwd")
>>> c.dir = "/dev"
>>> c.run()
>>> c.stdout
"/dev\n"

result

Represents the result of running an external command.

Attributes

NameTypeDescription
stdoutbyte_sliceThe standard output produced by the command.
stderrbyte_sliceThe standard error produced by the command.
pidintThe process ID of the command.

Examples

Example
>>> result := exec("ls")
>>> result.stdout
"file1\nfile2\n"