Skip to main content
Version: 0.19.0

Virtual-Sh Runtime

The virtual-sh runtime uses Invowk™'s built-in POSIX-compatible shell interpreter (powered by mvdan/sh). It provides consistent shell behavior across all platforms without requiring an external shell.

How It Works​

When you run a command with the virtual-sh runtime, Invowk:

  1. Parses the script using the built-in shell parser
  2. Executes it in an embedded POSIX-like environment
  3. Provides core utilities (echo, test, etc.) built-in

Basic Usage​

{
name: "build"
implementations: [{
script: {content: """
echo "Building..."
go build -o bin/app ./...
echo "Done!"
"""}
runtimes: [{name: "virtual-sh", allowed_binaries: ["go"]}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
invowk cmd build --ivk-runtime virtual-sh

Cross-Platform Consistency​

The virtual-sh runtime behaves identically on Linux, macOS, and Windows:

{
name: "setup"
implementations: [{
script: {content: """
# This works the same everywhere!
if [ -d "node_modules" ]; then
echo "Dependencies already installed"
else
echo "Installing dependencies..."
npm install
fi
"""}
runtimes: [{name: "virtual-sh", allowed_binaries: ["npm"]}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

No more "works on my machine" for shell scripts!

Built-in Utilities​

The virtual-sh shell includes core POSIX utilities:

UtilityDescription
echoPrint text
printfFormatted output
test / [Conditionals
true / falseExit with 0/1
pwdPrint working directory
cdChange directory
readRead input
exportSet environment variables

Extended Utilities (u-root)​

When enabled in config (default: true), 28 additional POSIX-compliant utilities from the u-root library are available. These include 12 upstream u-root wrappers and 16 custom implementations.

// In your config file
virtual: {
utilities: {
enabled: true
}
}

File Operations (14 utilities)​

UtilityDescriptionCommon Flags
base64Encode/decode base64-d (decode)
catConcatenate and display files-u (ignored, compatibility)
cpCopy files and directories-r (recursive), -f (force)
findSearch for files in a directory hierarchy-name (pattern), -type (f, d, l)
gzipCompress or expand files-d (decompress), -c (stdout), -f (force)
lnCreate hard or symbolic links-s (symbolic), -f (force)
lsList directory contents-l (long), -a (all), -R (recursive)
mkdirCreate directories-p (create parents)
mktempCreate temporary files or directories-d (directory), -p (prefix dir)
mvMove or rename files-f (force), -n (no clobber)
realpathResolve absolute path names(none)
rmRemove files and directories-r (recursive), -f (force)
tarArchive files-c (create), -x (extract), -f (file)
touchCreate or update file timestamps-c (no create)

Text Processing (10 utilities)​

UtilityDescriptionCommon Flags
basenameStrip directory and suffix from filenames(none)
cutSelect portions of lines-d (delimiter), -f (fields)
dirnameStrip last component from filenames(none)
grepSearch for patterns-i (ignore case), -v (invert), -n (line numbers)
headOutput first N lines-n <num> (default 10)
sortSort lines-r (reverse), -n (numeric), -u (unique)
tailOutput last N lines-n <num> (default 10)
trTranslate charactersSET1 SET2 (character mapping)
uniqFilter adjacent duplicate lines-c (count), -d (duplicates only)
wcCount lines, words, bytes-l (lines), -w (words), -c (bytes)

Other Utilities (4 utilities)​

UtilityDescriptionCommon Flags
seqGenerate number sequences-s (separator), -w (equal width)
shasumCompute SHA message digests-a (algorithm: 1, 256, 512)
sleepDelay for a specified time(none — takes duration argument)
teeDuplicate standard input to files-a (append)
note

These utilities implement POSIX behavior. GNU-specific flags (like --color, --time-style) are silently ignored for compatibility.

Error Identification​

Errors from u-root utilities are prefixed with [uroot] for easy identification:

[uroot] cp: /source/file: no such file or directory
[uroot] rm: /protected: permission denied

No Silent Fallback​

If a u-root command fails, it returns an error immediately. The system does not fall back to host binaries, ensuring bugs are caught rather than masked.

POSIX Shell Features​

The virtual-sh shell supports standard POSIX constructs:

Variables​

script: {content: """
NAME="World"
echo "Hello, $NAME!"

# Parameter expansion
echo "${NAME:-default}"
echo "${#NAME}" # Length
"""}

Conditionals​

script: {content: """
if [ "$ENV" = "production" ]; then
echo "Production mode"
elif [ "$ENV" = "staging" ]; then
echo "Staging mode"
else
echo "Development mode"
fi
"""}

Loops​

script: {content: """
# For loop
for file in *.go; do
echo "Processing $file"
done

# While loop
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
"""}

Functions​

script: {content: """
greet() {
echo "Hello, $1!"
}

greet "World"
greet "Invowk"
"""}

Subshells and Command Substitution​

script: {content: """
# Command substitution with built-ins
here=$(pwd)
echo "Current directory is $here"

# Subshell
(cd /tmp && echo "In temp: $(pwd)")
echo "Still in: $(pwd)"
"""}

Calling External Commands​

The virtual-sh runtime can call host commands only when an implementation explicitly opts in with allowed_binaries:

{
name: "status"
implementations: [{
script: {content: """
# Calls explicitly allowed host binaries
go version
git status
"""}
runtimes: [{
name: "virtual-sh"
allowed_binaries: ["go", "git"]
binary_lookup_mode: "host"
}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

By default, host binary execution is denied. Named entries such as go and git are resolved according to binary_lookup_mode; allowed_binaries: ["*"] is the explicit opt-out that allows every host binary.

:::caution Not a Sandbox The virtual-sh runtime is not a security sandbox. It is a portable shell interpreter augmented with built-in utilities and host-binary gates. Any allowed host binary (for example git, curl, python, or node) still executes as a native process with host access. By default, the host's environment variables are also inherited (env_inherit_mode: "all").

If you need execution isolation, use the container runtime instead. :::

File Access​

Redirections and the built-in utilities go through Invowk's path validator. By default (virtual.filesystem.access: "restricted") a path must resolve inside an allowed root: the command workdir (except a module workdir outside the module, below), the script or module source root, the @tmp, @config, @data, @cache and @state anchors, /tmp (except on Windows), and the selected platform's virtual.filesystem.paths entries (see Paths And Files).

  • Links are followed before the check. Every symbolic link and every Windows junction on the path is resolved, also when the last parts of the path do not exist yet.
  • A link that leaves the allowed roots is denied at any depth, with virtual path denied: mkdir -p link/a/b and echo x > dangling-link fail when the link points outside. A link or junction whose target stays inside an allowed root works.
  • A path that cannot be resolved is refused (a link cycle, a missing drive or share), and a virtual.filesystem.paths entry that cannot be resolved fails the command before the script runs.
  • A module's workdir outside the module is not an allowed root. When a module's own invowkfile (one that Invowk loads as part of a module) sets a workdir (at the root, command or implementation level) that resolves outside the module directory, with links and junctions followed, the script still starts there and @work still names it, but neither the workdir nor @work is an allowed root: reads, writes and cd there are refused, and file tests such as [ -f file ] are false. Host binaries from allowed_binaries still run there with host access. To read or write there from the script, declare the directory in the selected platform's virtual.filesystem.paths (for example WORK: "@work") or pass --ivk-workdir. The --ivk-workdir override, the workdir of an invowkfile that Invowk does not load as part of a module, and a module workdir inside the module stay allowed roots (see Working Directory).
  • /dev/stdout, /dev/stderr and /dev/stdin are not files when the stream is a pipe (Linux), so they cannot be opened as paths. Write echo msg >&2 or >&1 instead: a descriptor duplication opens no path.

With access: "full" there is no allowed-root check, and a path the validator cannot resolve is passed to the operating system as it is.

Under both access modes a path that resolves is used in its resolved form, except by the operations that act on the last component itself:

  • Paths are reported in resolved form. INVOWK_PATH_* values and virtual path denied messages show the target of a link or junction, not the link. For the operations that act on the link itself (below), they show the link's own path: its resolved directory joined with its name.
  • Most operations follow a link or junction to its target: reading and writing files, cd, cp, the destination of mv, and every file test except -L and -h. The mv destination is a known difference from a host shell: mv a link with link pointing to a file replaces that file and keeps the link, where a host shell replaces the link.
  • rm, the mv sources and ln's link name act on the link itself and do not follow the last component. rm link and rm -r link remove the link and keep its target, mv link dir moves the link, and ln -sf target link replaces an existing link, never its target. Under restricted access a link inside an allowed root can be removed or moved even when its target lies outside them; the target stays. On Windows rm -rf of a junction removes the junction and keeps its target tree. On macOS a restricted rm -r /tmp is refused, because the link /tmp lies outside the allowed root /private/tmp; rm /tmp/file works.
  • The -L and -h tests are true for a symbolic link, and false for its target (a Windows junction tests false).
  • A trailing separator asks for a directory. An operand of rm, mv or ln that ends in a separator, . or .. is resolved in full: rm -r dirlink/ removes the target directory and keeps the link. When it names a link to a file (rm live/, rm -r live/, mv live/ x, ln -sf t live/), the command fails with not a directory, naming the resolved path, and nothing changes. ln never replaces a directory: a link name that names one, in any form (dir, dir/, dir/., ., or link/ for a link to a directory), fails with cannot overwrite directory and removes nothing.
  • cd and the -r, -w and -x tests check access on the validated path, with links followed, so cd into a link or junction to a directory works. Under restricted access a path outside every allowed root makes those tests false, and cd to it fails with cd: virtual path denied.
  • The built-in utilities find their operands the way each one parses its arguments. -- ends the options, and every operand is checked, whatever it starts with. Option values pass as written, except the ones that name paths, such as tar -f, which are checked too, and so is the file gzip writes for each operand. gzip -S refuses a suffix that contains a path separator.
  • cp and tar -x check the paths they derive from their operands. Under restricted access every path they create, write or change the mode of (the destination directory joined with each source's name, every path below it for cp -r, every entry of a tar -x archive) and every source link cp follows without -P is resolved and must be inside an allowed root. One refusal refuses the whole command with virtual path denied, before anything is written. So that those paths are known in advance, a restricted cp refuses a later source that does not exist when it starts (a missing first source keeps its plain "no such file" error), cp -r does not write into a source tree of the same command (a directory copied into itself), cp -P refuses a copy that would create a link next to other entries (a copied tree's own directory is one of them, so cp -P -r of a tree that holds a link is refused), and tar -x reads its archive only from a regular file, never from standard input. A link named as a cp operand is resolved, in both access modes, so cp -P lnk d copies its target, and a restricted cp -P creates no link. access: "full" skips these derived-path checks; the operand is still resolved.

The check covers the interpreter's own file operations only: an allowed host binary runs as a normal host process and is not path-checked. Use the container runtime for isolation.

Environment Variables​

Environment variables work the same as in native, except OSC7577_TOKEN is stripped at virtual-runtime boundaries so restricted scripts cannot schedule arbitrary host binaries on terminal restart (use native for transparent host TUI launchers):

{
name: "build"
env: {
vars: {
BUILD_MODE: "release"
}
}
implementations: [{
script: {content: """
echo "Building in $BUILD_MODE mode"
go build -ldflags="-s -w" ./...
"""}
runtimes: [{name: "virtual-sh", allowed_binaries: ["go"]}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

Flags and Arguments​

Access flags and arguments the same way:

{
name: "greet"
args: [{name: "name", description: "Name to greet", default_value: "World"}]
implementations: [{
script: {content: """
# Using environment variable
echo "Hello, $INVOWK_ARG_NAME!"

# Or positional parameter
echo "Hello, $1!"
"""}
runtimes: [{name: "virtual-sh"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

Limitations​

No Non-Shell Interpreter Support​

The virtual-sh runtime cannot use non-shell script.interpreter values:

// This will NOT work with virtual-sh runtime!
// Runtime validation rejects non-shell script interpreters because
// virtual-sh always uses the embedded mvdan/sh interpreter.
{
name: "bad-example"
implementations: [{
script: {
content: """
#!/usr/bin/env python3
print("This won't work!")
"""
interpreter: "python3"
}
runtimes: [{name: "virtual-sh"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

For Python, Ruby, or other interpreters, use the native or container runtime.

Bash-Specific Features​

Some bash-specific features are not available:

// These won't work in virtual-sh runtime:
script: {content: """
# Bash arrays (use $@ instead)
declare -a arr=(1 2 3) # Not supported

# Bash-specific parameter expansion
${var^^} # Uppercase - not supported
${var,,} # Lowercase - not supported

# Process substitution
diff <(cmd1) <(cmd2) # Not supported
"""}

Stick to POSIX-compatible constructs for virtual-sh runtime.

Dependency Validation​

Root, command, and implementation dependencies are still validated on the host before virtual-sh runs. When a virtual-sh command launches host tools, list those tools in both depends_on.tools and allowed_binaries:

{
name: "build"
depends_on: {
tools: [
// Host dependencies are checked before virtual-sh runs.
{alternatives: ["go"]},
{alternatives: ["git"]}
]
}
implementations: [{
script: {content: "go build ./..."}
runtimes: [{name: "virtual-sh", allowed_binaries: ["go", "git"]}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

Advantages​

  • Consistency: Same behavior on Linux, macOS, and Windows
  • No shell dependency: Works even if system shell is unavailable (the interpreter is built in, but external commands still require explicit host-binary permission)
  • Portability: Scripts work across all platforms
  • Built-in utilities: Core utilities always available
  • Faster startup: No shell process to spawn

When to Use Virtual-Sh​

  • Cross-platform scripts: When the same script must work everywhere
  • CI/CD pipelines: Consistent behavior across build agents
  • Simple shell scripts: When you don't need bash-specific features
  • Embedded environments: When external shells aren't available

Configuration​

Configure shared virtual runtime utilities in your Invowk config file:

// ~/.config/invowk/config.cue (Linux)
// ~/Library/Application Support/invowk/config.cue (macOS)
// %APPDATA%invowkconfig.cue (Windows)

virtual: {
utilities: {
// Enable additional utilities from u-root
enabled: true
}
}

Next Steps​