# Shells and remote commands

> For the complete documentation index, see [llms.txt](https://docs.ringleader.dev/llms.txt).


> `rl shell` opens a shell on a workstation, or runs one command there and returns its output and exit code.


`rl shell` opens a shell on a workstation, or runs one command there and returns its output
and exit code. `rl tmux` opens a shell that survives a dropped connection. Both work with no
SSH setup. A script or a coding agent can run `rl shell` with a command, as well as a person.
`rl tmux` always attaches interactively, so it is for a person at a terminal.

## Workstation choice for shell and tmux

When you leave out the workstation's name, `rl shell` and `rl tmux` choose one for you if
exactly one fits. They look for these, in order:

- The only `Ready` workstation.
- When none is `Ready`, the only one that is running but not yet `Ready`. They warn you when
  they choose it.
- With `--start`, the only workstation, even a stopped one.

Otherwise they list the candidates and stop. Name the one you want with `-w`, or add `-A` to
look in every namespace, not only the current one.

`rl shell` chooses a workstation only when you give it no command. With a command and no
`-w`, it reads the first word as the workstation's name. To run a command, name the
workstation first or with `-w`: `rl shell -w my-box -- make test`.

## Arguments of a remote command

The workstation receives exactly the arguments you wrote, one word for one word. A
metacharacter inside an argument is data, not syntax, so
`rl shell my-box -- printf '%s|' a d` prints `a|d|`. The argument
`-H "Authorization: Bearer $TOKEN"` arrives as a single argument, not three. `docker exec`
and `kubectl exec` work the same way, which makes `rl shell` safe to drive from a script.

A remote shell needs no flag: run `sh -c` with the command line as one quoted argument. The
workstation's own shell then interprets the pipes, `&&`, redirections, globs and `~` inside
it:

```bash
rl shell my-box -- sh -c 'cd ~/src && make test 2>&1 | tail -20'
```

## Expansion of tilde and variables

Your shell expands `~`, `$HOME` and globs on your computer, before `rl` runs. By the time
`rl` holds the arguments, `~/src` has already become `/home/you/src`, and nothing can tell
the two apart afterward.

So `rl shell my-box -- ls ~` asks the workstation to list your computer's home
path. Quoting it as `rl shell my-box -- ls '~'` sends a literal `~`, which fails
with `ls: cannot access '~': No such file or directory`. To expand it on the workstation, use
`rl shell my-box -- sh -c 'ls ~'`, or pass an absolute path.

## Environment of a remote command

A remote command runs in a non-login, non-interactive shell. `/etc/profile`,
`/etc/profile.d/*` and `~/.profile` are not read, so anything exported from a profile
fragment is missing. That covers a `PATH` entry that a devtool adds and shell integrations
such as `fzf`.

Two things reach the command anyway, because neither travels by a profile fragment:

- The variables you declare under `environment:` in a WorkstationConfig. Ringleader also
  writes them to `/etc/environment`, which the login system reads on every SSH session.
- Your forwarded SSH agent, as
  [SSH keys for outbound Git](https://docs.ringleader.dev/workstations/connectivity/git-ssh-keys/#ssh-agent-in-every-session)
  explains.

To get the rest, wrap the command in a login shell:

```bash
rl shell my-box -- bash -lc 'node --version'
```

## Terminal for a remote command

An interactive shell has a terminal. A remote command runs without one, and some programs
need one anyway: an editor, a pager, or a command that ends at a shell prompt.
`--pty` gives the remote command a terminal.

A terminal merges the command's standard error into its standard output. Use `--pty` for a
command you are driving by hand, and leave it off for one whose output you are capturing.
`--pty` does not combine with `--script` or `--copy`. Asking for both stops with an error.

## Local script files

`--script` takes a script that lives on your computer, copies it to the workstation under its
own name, and runs it there. Its exit code becomes the command's exit code.

The script's bytes are never text that a shell parses. They travel as a file, are unpacked as
a file and run as a file. A backtick, `$(...)`, `|`, `*` or a newline inside the script means
what it would mean if you had typed the file on the workstation. A heredoc
(`-- bash -s <<EOF`) can't give you that. Your local shell evaluates an unquoted heredoc before
the bytes leave, so a backtick in a comment is enough to run something on your computer.

`--copy` sends a local directory along with the script and runs the script from inside the
copied directory. The script must be inside the directory you copy. Neither flag combines with
a trailing command: a run is either a remote command or a script file, never both.

## Persistent sessions with tmux

`rl tmux` attaches to a `tmux` session named `ringleader` on the workstation, and creates it
the first time. When the connection drops, it reconnects after a short wait and attaches to
the same session, so long-running work survives a flaky connection.

## Shell location in a devcontainer workstation

On a workstation that runs a devcontainer, `rl shell`, a command, a script and `rl file cp`
all land inside the container. To land on the workstation's virtual machine instead, see
[Devcontainer shell and file locations](https://docs.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-shell-and-file-locations),
which covers `--host` and the login for a shell there.

## Open a shell

To open a shell, click **Open shell** on the workstation's card in the application, or run:

```bash
rl shell my-box -n local
```

Type `exit` to leave. With exactly one `Ready` workstation, you can leave out its name.

## Run a command

To run one command and get its output, put it after `--`:

```bash
rl shell my-box -n local -- uname -a
```

The workstation's output goes to your standard output, and the command's exit code becomes
the exit code of `rl shell`. Every message from Ringleader goes to standard error, so a script
can capture the output on its own.
[`rl shell`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-shell-workstation-command) lists the exit
codes Ringleader uses for its own failures and how `--exit-code-file` tells them apart.

## Start a workstation as you connect

To start a stopped workstation as you connect, add `--start`:

```bash
rl shell --start my-box -n local -- make test
```

Everything `--start` prints goes to standard error, so your standard output carries only the
command's output.
[Start a workstation when you connect](https://docs.ringleader.dev/workstations/lifecycle/starts-stops-restarts/#start-a-workstation-when-you-connect)
says what it does when the workstation can't start.

## Give a command a terminal

To run a command that needs a terminal, add `--pty`:

```bash
rl shell --pty my-box -n local -- bash -lc 'cd ~/src/worktree && exec $SHELL -l'
```

That command moves to a worktree and replaces itself with a login shell. You land at a prompt
that is already in the right directory.

## Run a local script file

To run a script from your computer on the workstation, name it with `--script`:

```bash
rl shell my-box -n local --script ./setup.sh
```

To send a directory along with it, add `--copy`. The script must be inside that directory:

```bash
rl shell my-box -n local --copy ./demo --script ./demo/run.sh
```

## Attach to a persistent session

To attach to the `ringleader` session, run:

```bash
rl tmux my-box -n local
```

Add `--start` to start the workstation first, as `rl shell --start` does.

## Next steps

- [`rl shell`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-shell-workstation-command) lists every
  flag, the exit codes and `--exit-code-file`.
- [`rl tmux`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-tmux-workstation) lists the flags of the
  persistent session command.
- [`rl logs`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-logs-workstation) streams the in-VM
  agent's logs, or a managed service's, for a workstation you can't reach with a shell.
- [Editors and plain SSH](https://docs.ringleader.dev/workstations/connectivity/editors-ssh/) connects
  your own SSH tools and your editor to a workstation.

