Rash - Rust to Shell Transpiler
Rash transpiles a safe subset of Rust to POSIX-compliant shell scripts, enabling you to write maintainable and type-safe shell scripts using familiar Rust syntax.
Why Rash?
- ๐ก๏ธ Safety First: Automatic protection against shell injection attacks
- ๐ Compile-Time Verification: Catch errors before deployment
- ๐ฆ Zero Runtime Dependencies: Generated scripts work on any POSIX shell
- ๐ฏ Deterministic Output: Same input always produces identical scripts
- โ ShellCheck Compliant: All output passes strict linting
Quick Start
Write Rust:
// install.rs
Get POSIX shell:
#!/bin/sh
# Generated by Rash v0.4.0
# POSIX-compliant shell script
IFS='
'
# Rash runtime functions
# Main script begins
# Execute main function
Installation
From crates.io (Recommended)
Binary Releases
Pre-built binaries are available for Linux and macOS:
# Linux x86_64
|
# macOS x86_64
|
# macOS ARM64
|
Using cargo-binstall
From Source
# Full build with all features
# Minimal build (smaller binary, ~2MB)
Usage
Basic Commands
# Transpile a Rust file to shell
# Check if a file is valid Rash
# Verify safety properties
# Start interactive playground (if built with playground feature)
CLI Options
)
)
)
)
Language Features
Supported Rust Subset
Rash supports a carefully chosen subset of Rust that maps cleanly to shell:
Variables and Types
let name = "Alice"; // String literals
let count = 42; // Integers (including negatives: -42)
let flag = true; // Booleans
let user = env; // Environment variables
let result = capture; // Command output
Arithmetic Operations
let x = 1 + 2; // Addition โ $((1 + 2))
let y = 10 - 3; // Subtraction โ $((10 - 3))
let z = 4 * 5; // Multiplication โ $((4 * 5))
let w = 20 / 4; // Division โ $((20 / 4))
Comparison Operators
if x > 0
if y == 5 // Equal โ [ "$y" -eq 5 ]
if z < 10 // Less than โ [ "$z" -lt 10 ]
User-Defined Functions
Built-in Functions
// I/O operations
echo; // Print to stdout
println!; // println! macro (Sprint 10)
eprint; // Print to stderr
// File system
mkdir_p; // Create directory recursively
write_file; // Write file
let content = read_file; // Read file
if path_exists // Check path
// Process management
exec; // Run command
let output = capture; // Capture command output
exit; // Exit with code
// Environment
set_env; // Set environment variable
let val = env; // Get environment variable
let val = env_var_or; // With default
Control Flow
// Conditionals
if condition else if other else
// โ ๏ธ Pattern matching - Not yet supported (P2 backlog)
// match value {
// "linux" => echo("Linux detected"),
// "darwin" => echo("macOS detected"),
// _ => echo("Unknown OS"),
// }
Loops
// โ ๏ธ For loops - Not yet supported (P2 backlog)
// for i in 0..10 {
// echo("Iteration: {i}");
// }
// โ ๏ธ While loops - Not yet supported
// let mut count = 0;
// while count < 10 {
// count = count + 1;
// }
Safety Features
All generated scripts are protected against:
- Command Injection: All variables are properly quoted
- Path Traversal: Paths are validated and escaped
- Glob Expansion: Glob patterns are quoted when needed
- Word Splitting: IFS is set to safe value
- Undefined Variables:
set -ucatches undefined vars
Example of automatic safety:
let user_input = env;
exec; // Safe: becomes echo "$user_input"
Examples
See the examples/ directory for complete examples:
-
Basic
- Hello World - Simplest example
- Variables - Variable usage and escaping
- Functions - Built-in functions
-
Control Flow
- Conditionals - If/else statements
- Loops - Bounded iteration
-
Safety
- Injection Prevention - Security examples
- String Escaping - Special character handling
-
Real-World
- Minimal Installer - Bootstrap script
Shell Compatibility
Generated scripts are tested on:
| Shell | Version | Status |
|---|---|---|
| POSIX sh | - | โ Full support |
| dash | 0.5.11+ | โ Full support |
| bash | 3.2+ | โ Full support |
| ash (BusyBox) | 1.30+ | โ Full support |
| zsh | 5.0+ | โ Full support |
| mksh | R59+ | โ Full support |
Performance
Rash is designed for fast transpilation:
- 21.1ยตs transpile time for simple scripts (100x better than target!)
- Memory usage <10MB for most scripts
- Generated scripts add minimal overhead (~20 lines boilerplate)
Quality Metrics (v0.4.0)
| Metric | Status |
|---|---|
| Tests | 520/520 โ |
| Property Tests | 23 (~13,300 cases) โ |
| Coverage | 85.36% โ |
| Complexity | <10 cognitive โ |
| Performance | 21ยตs โ |
| ShellCheck | 24/24 pass โ |
| Edge Cases Fixed | 7/11 (64%) ๐ก |
Production Ready: All P0 (critical) and P1 (high priority) edge cases resolved.
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
# Clone the repository
# Run tests
# Run with all checks
# Build release binary
License
Rash is licensed under the MIT License. See LICENSE for details.
Acknowledgments
Rash is built with safety principles inspired by:
- ShellCheck for shell script analysis
- Oil Shell for shell language design
- The Rust community for memory safety practices
Roadmap
Completed (v0.4.0):
- Core transpilation engine
- Basic safety validation
- ShellCheck compliance (24/24 tests)
- Property-based testing (23 properties, ~13,300 cases)
- Arithmetic expressions (
$((expr))) - Function return values
- Negative integers
- Integer comparisons
-
println!macro - Global function scope
- MCP server (rash-mcp)
In Progress:
- For loops (P2 - parser work required)
- Match expressions (P2 - pattern matching)
Future:
- Language server (LSP)
- Incremental compilation
- More shell targets (fish, PowerShell)
- Package manager integration
See ROADMAP.md for detailed sprint planning.