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.
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
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.57andzsh@5.9are checked against pinned SHA-256 sums; other versions install with a warning. - Option profile
strict(set -euo pipefail),posixorinteractive. Fish activation uses a.fishvariant; shellenv doesn't install fish.- PATH
- The env's
bin/first, then the pinned runtime, then your usualPATH. A tool inbin/shadows the system copy. - Home and temp dirs
HOME,TMPDIRand theXDG_*config, cache, data and state dirs point into the env'shome/. Always withexec; withactivatewhen 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.
| Behaviour | eval "$(shellenv activate)" |
shellenv exec -- <cmd> |
shellenv exec --container <image> -- <cmd> |
|---|---|---|---|
Prepends env bin/ to PATH | yes | yes | yes (inside container) |
Sets SHELLENV_* variables | yes | yes | yes (inside container) |
Sandboxes HOME / TMPDIR | opt-in (--isolate-home) | yes (./.shellenv/<env>/home/) | yes (mounted on container) |
| Network / Process isolation | no | no | yes (container namespaces) |
| System filesystem isolation | no | no | yes (container namespaces) |
| Changes current shell session | yes (until you reset it) | no (subprocess only) | no (container execution) |
| Propagates command exit code | n/a | yes | yes |
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.
-
Create the global home
A temp
SHELLENV_HOMEkeeps the real~/.shellenvuntouched while you try things.export SHELLENV_HOME="$(mktemp -d)/home" shellenv init Initialized shellenv at …/home -
Declare the shell and profile
Writes
./.shellenv/default/with its metadata,bin/andhome/.shellenv create --shell bash@5.2 --profile strict Created env "default" for shell bash@5.2 at …/demo/.shellenv/default -
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 -
Run a script under the profile
--profilesources the strict profile first. Itsset -ecovers 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 -
Activate, then deactivate
deactivaterestoresPATH, the prompt and any isolated variables. It can't undo shell options: after the strict profile,set -euo pipefailand itsIFSstay 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 setSHELLENV_PROFILESto 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 installneeds a C compiler,makeandtar, and takes a few minutes per version.shellenv doctorchecks for them.