shellenv

Per-project shell sandboxes.

Pin the shell version and option profile a project expects. shellenv exec points HOME, TMPDIR and XDG_* into the project, so writes that go through them stay out of your real home.

bash
shellenv create --shell bash@5.2 --profile strict
Created env "default" for shell bash@5.2 at …/demo/.shellenv/default
shellenv exec -- printenv HOME TMPDIR
…/demo/.shellenv/default/home
…/demo/.shellenv/default/home/tmp
envdefault shellbash@5.2 profilestrict
Real output from a local build of shellenv v0.3.0, with bash@5.2 already installed. Paths are shortened to …/.

What gets pinned

One script can behave differently across shells. Each project env lives in ./.shellenv/<env>/ and records the conditions it should run under.

Shell runtime
Built from the official source tarball. bash@5.2, bash@3.2.57 and zsh@5.9 are checked against pinned SHA-256 sums; other versions install with a warning.
Option profile
strict (set -euo pipefail), posix or interactive. Fish activation uses a .fish variant; shellenv doesn't install fish.
PATH
The env's bin/ first, then the pinned runtime, then your usual PATH. A tool in bin/ shadows the system copy.
Home and temp dirs
HOME, TMPDIR and the XDG_* config, cache, data and state dirs point into the env's home/. Always with exec; with activate when you pass --isolate-home.

Three ways to run

The three modes differ in how far the isolation goes. The env is the one you name, else ./.shellenv/current (set by shellenv use), else default.

What each mode isolates, as in README.md
Behaviour eval "$(shellenv activate)" shellenv exec -- <cmd> shellenv exec --container <image> -- <cmd>
Prepends env bin/ to PATHyesyesyes (inside container)
Sets SHELLENV_* variablesyesyesyes (inside container)
Sandboxes HOME / TMPDIRopt-in (--isolate-home)yes (./.shellenv/<env>/home/)yes (mounted on container)
Network / Process isolationnonoyes (container namespaces)
System filesystem isolationnonoyes (container namespaces)
Changes current shell sessionyes (until you reset it)no (subprocess only)no (container execution)
Propagates command exit coden/ayesyes

activate

An interactive session with the env's tools and profile. Leave it with eval "$(shellenv deactivate)", which restores the environment but not shell options the profile set.

eval "$(shellenv activate)"

Fish pipes it instead:

shellenv activate --shell-type fish | source

exec

One-off or scripted runs. Writes through HOME, TMPDIR and XDG_* land in the sandbox, and the exit status passes through. --ephemeral deletes the sandbox home afterwards.

shellenv exec -- ./steps.sh

exec --container

For scripts that change the system. Runs in Docker or Podman with the project mounted at the same path. The image supplies the shell, so --strict-shell is rejected.

shellenv exec --container debian:stable-slim -- ./steps.sh

Know the boundary. Host mode (activate and exec) is a user-space sandbox, not a security sandbox for untrusted code. exec redirects writes that go through HOME, TMPDIR and XDG_*; activate does so only with --isolate-home. The network, processes and the rest of the filesystem stay shared unless you use --container. ARCHITECTURE.md has the details.

Walkthrough

From an empty project directory. Output is from a real run, with paths shortened to …/. Lines starting with 2> went to stderr.

  1. Create the global home

    A temp SHELLENV_HOME keeps the real ~/.shellenv untouched while you try things.

    export SHELLENV_HOME="$(mktemp -d)/home"
    shellenv init
    Initialized shellenv at …/home
  2. Declare the shell and profile

    Writes ./.shellenv/default/ with its metadata, bin/ and home/.

    shellenv create --shell bash@5.2 --profile strict
    Created env "default" for shell bash@5.2 at …/demo/.shellenv/default
  3. Build the runtime

    Until it's installed, commands fall back to the system shell with a warning, or fail with --strict-shell. The build runs once per version.

    shellenv exec -- true
    warning: declared shell "bash@5.2" is not installed; falling back to the system shell (run 'shellenv install bash@5.2' or use --strict-shell to enforce)
    shellenv install bash@5.2
    Downloading https://ftp.gnu.org/gnu/bash/bash-5.2.tar.gz...
    Extracting bash-5.2.tar.gz...
    Running ./configure --prefix=…/home/installs/bash/5.2 CFLAGS=-O2 -std=gnu11 CFLAGS_FOR_BUILD=-O2 -std=gnu11...
    Running make -j4...
    Running make install...
    Installed bash@5.2 into …/home/installs/bash/5.2
  4. Run a script under the profile

    --profile sources the strict profile first. Its set -e covers what that shell runs directly, so source the script with .. Run through its own shebang, the script gets a fresh shell and only the profile's exported variables.

    printf '#!/bin/sh\necho "step 1"\nfalse\necho "step 2"\n' > steps.sh
    chmod +x steps.sh
    shellenv exec -- ./steps.sh; echo "exit $?"
    step 1
    step 2
    exit 0
    shellenv exec --profile -- . ./steps.sh; echo "exit $?"
    step 1
    exit 1
  5. Activate, then deactivate

    deactivate restores PATH, the prompt and any isolated variables. It can't undo shell options: after the strict profile, set -euo pipefail and its IFS stay in effect in that shell, so try this in a throwaway one.

    eval "$(shellenv activate)"
    command -v bash
    …/home/installs/bash/5.2/bin/bash
    eval "$(shellenv deactivate)"
    command -v bash
    /usr/bin/bash

Install

A single Go binary for Linux and macOS. On Windows, use the Linux build inside WSL.

From a release

Download a tarball from the releases page and check it against SHA256SUMS. Replace <version> with the one you downloaded.

tar -xzf shellenv-<version>-linux-amd64.tar.gz
export PATH="$PWD/shellenv-<version>-linux-amd64:$PATH"
shellenv --version
  • Keep the binary beside its profiles/ directory, or set SHELLENV_PROFILES to it.
  • Man pages are in man/: man -l man/shellenv.1.

From source

Needs Go 1.22 or later and make.

git clone https://github.com/systemhalted/shellenv.git
cd shellenv
make build
./dist/shellenv --version
  • Run from the checkout, it finds the built-in profiles in ./profiles.
  • shellenv install needs a C compiler, make and tar, and takes a few minutes per version. shellenv doctor checks for them.