> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Mirage Shell

> Run Bash-style commands, pipelines, and scripts across your mounted data.

Mirage's shell combines commands from [Core Tools](https://docs.mirage.strukto.ai/home/core/tools.md) and [Data & Network](https://docs.mirage.strukto.ai/home/core/data-network.md) with variables, control flow, and persistent sessions.

## Syntax

| Feature | Supported |
| - | - |
| Command lists | `;`, newlines, `&&`, `\|\|`, and `!` |
| Pipelines | `\|` and `\|&`; `$?` and `${PIPESTATUS[@]}` report exit statuses. With job control off, `shopt -s lastpipe` runs the final foreground stage in the current shell |
| Redirection | `>`, `>>`, `<`, `<>`, `>\|`, `2>`, `2>&1`, `&>`, `&>>`; descriptor duplication and closing |
| Inline input | Here-documents `<<EOF`, tab-stripping `<<-EOF`, and here-strings `<<<` |
| Quoting | Single quotes, double quotes, backslash escapes, and `$'...'` |
| Expansion | Variables, `${name:-default}`, substring and pattern operations, `$(command)`, backticks, and `$((expression))` |
| Paths | Tilde expansion, brace expansion, `*`, `?`, and bracket patterns. `GLOBIGNORE` excludes matching pathname spellings |
| Extended patterns | `extglob`, `globstar`, `dotglob`, `nullglob`, and `failglob` through `shopt` |
| Variables | Scalars, indexed arrays, associative arrays (`declare -A`), positional arguments, `$RANDOM`, `${FUNCNAME[@]}`, and the file names in `${BASH_SOURCE[@]}` |
| Conditions | `if` / `elif` / `else`, `test`, `[ ... ]`, and `[[ ... ]]`. `shopt -s nocasematch` enables case-insensitive shell pattern and regex comparisons |
| Loops | `for`, arithmetic `for`, `while`, `until`, and `select`; `break` and `continue` |
| Branching and functions | `case`, `name() { ...; }`, `function`, and `return` |
| Groups | `{ ...; }` shares the session; `( ... )` runs in an isolated session copy |
| Process substitution | `<(command)` and `>(command)` as live `/dev/fd/N` pipes, streamed both ways. `$!` holds the last substitution's PID for `wait "$!"` |
| Background work | `command &`, `jobs`, `wait`, `kill`, `disown`, and `ps` |
| Timing | `time`, `time -p`, and `TIMEFORMAT`; CPU times are reported as zero |
| Comments | `#` outside quotes |

## Shell commands

These are common options, not an exhaustive flag reference.

| Commands | Purpose and common options |
| - | - |
| `cd`, `pwd` | Change or print the working directory; `-L`, `-P` |
| `export`, `unset` | Export variables or functions (`export -f`); remove variables or functions (`unset -v`, `-f`) |
| `declare`, `typeset`, `local`, `readonly` | Set variable scope and attributes; arrays `-a` / `-A`, integers `-i`, readonly `-r`, export `-x` |
| `set` | Set positional arguments and options such as `-e`, `-u`, `-x`, `-f`, `-C`, and `-o pipefail` |
| `shopt` | Set, unset, print, or query options with `-s`, `-u`, `-p`, `-q`; `-o` selects `set` options |
| `alias`, `unalias` | Define or remove aliases; enable expansion with `shopt -s expand_aliases` |
| `read` | Read into variables; `-r`, `-a ARRAY`, `-d CHAR`, `-n N`, `-N N`, `-u FD` |
| `mapfile`, `readarray` | Read into an array; `-t`, `-d CHAR`, `-n N`, `-s N`, `-O INDEX`, `-u FD`, `-C CALLBACK`, `-c N` |
| `echo`, `printf` | Print text or formatted values; `echo -n/-e/-E`, `printf -v NAME` |
| `source`, `.`, `eval` | Run a file or command string in the current session. With `sourcepath` enabled, `source` searches `PATH` for a bare filename |
| `bash`, `sh` | Run a script, `-c COMMAND`, or stdin with `-s` in a fresh shell environment |
| `exec` | Replace the current shell command flow, or keep descriptor redirections; `-c` clears the command's environment |
| `getopts`, `shift`, `let` | Parse script options, shift positional arguments, or evaluate integer expressions |
| `trap` | Run actions on `EXIT`, `ERR`, and `RETURN` |
| `umask` | Read or set the creation mask; `-S` prints symbolic form |
| `command`, `type`, `which` | Run or locate a command; `command -v/-V`, `type -t/-a/-p/-P` |
| `true`, `false`, `:` | Return success or failure; `:` is a no-op |
| `exit` | End the current shell with an optional exit status |
| `history` | Read or manage command history; `-c`, `-d`, `-s`, `-p`, `-a`, `-r`, `-w`, `-n` |
| `help` | Show help for a shell builtin: `help`, `help cd` |
| `man` | Show command help, including registered CLI subcommands: `man ls`, `man linear issue create` |

## Sessions and permissions

`ws.shell(...)` keeps the session's working directory, variables, and functions between calls. Supplying `cwd` or `env` uses an isolated copy for that call. Calls on one session run in order; use separate sessions for concurrent command streams. Python accepts a cancellation event through `cancel`; TypeScript accepts an abort `signal`.

### Jobs across instances

A workspace runs as several instances when more than one process builds it
under the same `workspace_id` on one shared
[store](https://docs.mirage.strukto.ai/home/infrastructure/overview.md#workspace-state), as
[Infrastructure](https://docs.mirage.strukto.ai/home/infrastructure/jobs.md#several-instances-of-one-workspace)
shows. One of them runs its lines at a time, the first to run one; a line on
any other is refused with an error naming it until it closes the workspace.
Process IDs continue from the highest the workspace has used, so an instance
that takes over never reuses one.

If the running instance goes away without closing the workspace, another
can run its lines about 30 seconds later. It reports the jobs left behind as
ended with status 129, as Bash reports a hangup, and stops what they ran
outside Mirage: a sandbox program through a runtime on the same machine,
or a host program on the same machine whose PID still matches.

A program a runtime runs outside Mirage is a process of its own. `ps` shows
its real pid and the machine it runs on, a host program's host PID or a
[machine](https://docs.mirage.strukto.ai/home/setup/machine/overview.md#processes-and-jobs)'s. Mirage's own
commands show `mirage`:

```
    PID MACHINE                         CMD
4194305 mirage                          ps
4194306 mirage                          sleep 300
  19443 host:my-laptop                  python3 train.py
```

Mirage's pids start at 4194305, past any Linux pid, so `$!`, `kill`, `wait` and
`ps -p` take either kind.

### Per-session mount modes

Session mount modes can narrow access to `read`, `write`, or `exec` (`r`, `rw`, `rwx`). Unlisted mounts keep their configured modes. Interpreters such as `python3` and `js` require `exec`; shell scripts run with the permissions of each command they contain. Use an [session profile](https://docs.mirage.strukto.ai/home/configuration/session.md) to hide paths or restrict commands.

## Limits

* Positive `read -t` values are accepted but do not enforce a timeout.
* `bg`, `complete`, `compgen`, `ulimit`, `coproc`, and `exec -a/-l` are not implemented.
* A process substitution's `/dev/fd/N` path belongs to the command that opened it. An input producer can keep running after that command finishes. The command waits for output consumers. Opening an output substitution with `exec 3> >(command)` fails with exit status `2`.
* There is no interactive terminal or OS process job control. The supported job commands manage Mirage jobs: `cmd &`, `jobs`, `wait` and `kill`.
* `kill` takes every Linux signal `kill -l` lists, by name or number. `STOP` pauses a job, which `ps` shows as `T`, and `CONT` lets it go on; a `TERM` sent meanwhile waits for the `CONT`. A Mirage command pauses at its next file operation, pipe write, sleep or loop turn, and in a browser `STOP` of one is refused. A Monty or Pyodide program pauses where it is, and a QuickJS or WASI program at its next file operation or output. Any other signal whose default is to be ignored or to pause a process (`CHLD`, `URG`, `WINCH`, `TSTP`, `TTIN`, `TTOU`) succeeds and changes nothing. A Pyodide program gets the signal as sent too, as does a program a runtime started on a machine or on the host: one that handles it runs on, and the job ends when the program exits, with its status. `KILL` is sent only for `kill -9`, `timeout -k`, or when the workspace shuts down. Any other process stops, and `wait` reports 128 plus the signal, as in Bash. As in Bash, a background job ignores `INT` and `QUIT`. Unlike Bash, `kill` also stops the jobs a job started and theirs, even once the job between them has ended.
* When time runs out, `timeout` signals its command's process group, as GNU `timeout` does, so the jobs the command started stop too. It doesn't with `--foreground`, `-s INT` or `-s QUIT`.
* A background job that gets `INT` acts as in Bash:
  * a job that is one program ignores it;
  * a job that runs several commands stops only its current command, subshell or pipeline, and goes on;
  * a job that runs a function ends.
* Every line of a session runs as one shell process, `$$`, which leads the session's process group; its jobs, pipeline stages, subshells, command substitutions and nested shells are in it. A negative operand signals a whole group (`kill -TERM -- -$$`). A signal that stops `$$` ends the line with 128 plus the signal and leaves its jobs running. As in Bash, a shell ignores `QUIT`, which stops only the program it is running. A nested `bash -c` is a process of its own, with its own `$$`.
* The shell's own errors start as a non-interactive Bash's do, `bash: line N: `, with `environment` in place of `bash` inside a function and the file's path inside a sourced file or a script. `$LINENO` is the running command's line and `$BASHPID` the process running it. When a signal ends a command, the shell prints Bash's report line after it, such as `bash: line 1:    12 Killed                  sleep 3`; for a background job, after the next program, subshell or pipeline it runs, or at `wait`. `TERM` prints `Terminated` for a command and nothing for a job, and `INT` and `PIPE` print nothing. A program a runtime runs in the foreground is not a process of its own, so no report line follows when a signal ends it; its status is still 128 plus the signal. A command substitution reports nothing.
* A program a sandbox runs, or one a `host` machine captures besides its own `python3`, writes its output and its stderr into a redirect's file as it prints them. So do most of Mirage's commands, such as `cat`, `grep`, `awk`, `sed`, `tee`, `sort` and `jq`, and the commands in a group, a loop or a function (`{ echo a; sleep 9; } > f`). A command that reads or changes that file meanwhile finds what was written before it, and `cat` and `grep` refuse to read the file their own output goes to, as GNU's do. The output follows the file through a rename the line makes, and the rest is lost once the line removes it or renames another file over it, as in Bash; a rename made from another session or through FUSE is not followed. What a stopped command wrote stays in the file, whether `kill`, `timeout` or a cancelled call stopped it. Others, such as `tar`, `gzip` and a `host` machine's `python3`, write theirs when they end, as do a program a Daytona sandbox runs and a microsandbox program given piped input. A file on a RAM mount shows a streaming write within a tenth of a second; an object store such as S3 publishes it when the write ends.
* `set` and `shopt` recognize more option names than they implement; accepting or displaying an option does not mean every Bash behavior is available.
* Enable `extglob` before the call that uses its syntax, or use `eval` after enabling it. A whole `shell()` call is parsed before it runs.
* Command substitutions and background jobs do not inherit stdin. Writes through an already open output descriptor append rather than track a write offset.
* Syntax errors return exit status `2`. Compatibility is a supported subset of Bash, not a full Bash installation.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.