Running commands

Running commands: shell

shell runs a command, the way you would type it in a terminal, waits for it to end, and gives what it wrote:

files: str = shell("ls -la")
print(files)

branch: str = shell("git branch --show-current")
print("on ${branch}")

The result is what the command wrote to its output (stdout), as a str, without the line breaks at its end: branch is main, not main\n. To go through the output line by line, split it:

shell("ls").split("\n").walk(name) {
    print(name)
}

On its own line, the output is dropped. That’s for commands you run for what they do:

shell("mkdir -p backups")

The command line

shell("...") hands the text to the system’s shell: sh on Linux, cmd on Windows. Everything the shell can do works, such as pipes, * and &&:

count: str = shell("ls *.log | wc -l")

This also means the text is written for that system: ls -la works on Linux but not with cmd on Windows, where it is dir. env.os() tells which system the program runs on, so it can choose.

A program and its arguments

Give shell a list, and it runs the program with exactly those arguments, without a shell in between:

log: str = shell(["git", "log", "-n", "5", "--oneline"])

Use this form whenever part of the command comes from outside: from a user, a file or a web request. With a command line, a name like x; rm -rf ~ would run the second command too:

name: str = prompt("File: ")
shell("cat ${name}")        // dangerous: the shell reads ; and $ and spaces in name
shell(["cat", name])        // safe: name is one argument, whatever is in it

The program is looked for in the folders of PATH, as a shell does. A name with a / in it is a path: shell(["./build.sh"]).

When a command fails

A command says it failed with an exit code other than 0. shell then raises error 16, with the code and what the command wrote to its error output (stderr):

e: error = nil
try e {
    shell("ls /nope")
}
print(e.code)
print(e.message)

Output:

16
command failed (exit 2): ls: cannot access '/nope': No such file or directory

A program that isn’t there, or can’t be run, is error 16 too: cannot run gti: not found. Without try, the error stops the program, at the line of the shell, like any other error.

Talking to a program: shell.start

shell waits for a command to end. shell.start starts a program that runs beside yours, and gives a process: you write to its input (stdin) and read its output (stdout) while it runs, the way you talk to a server over tcp. This is how a program uses a tool that answers line by line, a language server, or an MCP server.

p: process = shell.start(["cat"])
p.write("hello\n")
print(p.line())
p.write("world\n")
print(p.line())
p.close()
print(p.wait())

Output:

hello
world
0
MethodWhat it does
shell.start(list)starts the program with exactly these arguments (found in the folders of PATH, as for shell)
p.write(x)writes a str, a [byte] or a buffer to its input
p.line()the next line of its output, without its \n
p.read(n), p.read()exactly n bytes of its output, or all of it until it ends; a str, [byte] or buffer, whichever it goes into
p.timeout(seconds)how long a read or write may wait at most (error 14 after it); 0 takes it away
p.close()ends its input: a program that reads until the end of its input stops then
p.wait()waits for it to end and gives its exit code (128 and the signal’s number when a signal ended it); on its own line the code is dropped
p.kill()stops it at once
  • Reading and writing work like a tcp connection’s, with the same errors (14): the connection to cat closed before a line when its output ended without one.
  • What it writes to stderr shows where your program’s does: its messages and logs aren’t lost.
  • It sees the environment variables and the folder of your program, as shell’s commands do.
  • While one process is waited for, other work goes on: several run at once in wait(list, f), and a server’s handlers go on.
  • A process doesn’t outlive your program. One that still runs when nothing holds it anymore is stopped, and so is every one when the program ends. (In luma run, a process nothing holds runs until the program ends.)
  • A program that isn’t there, or can’t be run, is error 16, as for shell.

Details

  • The output comes when the command ends. shell waits for the command and gives everything at once. What it writes to stderr isn’t shown, but becomes part of the error when the command fails.
  • The command reads nothing. Its input (stdin) is empty, so a command that asks something gets no answer and usually ends.
  • The command sees the environment variables of the program, including what env.set set.
  • Commands run at the same time in wait(list, f): while one waits for its command, the others go on.
  • A command and its arguments can’t have NUL bytes ("\0") in them (error 16).
  • On Windows, a command line runs with cmd /d /s /c, and a list’s arguments are quoted so that the program reads them back as they were.