A cross-platform implementation of the iconic falling rain animation from the Matrix movies with rainbow colors, inspired by cmatrix | lolcat.
- Bash (
matrix-rain.sh) - Pure bash, no compilation needed - Go (
main.go) - Compiled binary for Linux, macOS, and Android Termux
This was originally created in ~ 2020, during COVID, after rewatching all the Matrix movies. This new update adds a high-performance Go implementation with cross-platform support - with the help of Claude + Codex
✨ Rainbow Colors - Smooth 24-bit RGB gradient animations cycling through the entire spectrum using sine wave algorithm (similar to lolcat)
🎌 Katakana Characters - Authentic Matrix characters (ア, イ, ウ, エ, etc.) falling from the top of the screen
🌊 Trail Effects - Dynamic brightness trails:
- Bright/bold head character at the bottom of each stream
- Medium brightness middle trail
- Dim fading character above
- Automatic erasure beyond trail length
⚙️ Customizable Speed - Control animation speed from 1 (slow) to 10 (fast)
📊 Adjustable Density - Control column density as a percentage of terminal width (1-100%)
🎯 Terminal Compatibility - Works with modern terminals supporting 24-bit true color (COLORTERM)
🔄 Responsive - Handles terminal resize events (SIGWINCH) dynamically
- Bash 4.0+ (for associative arrays and process management)
- awk (for floating-point sine wave calculations)
- Terminal with 24-bit true color support (most modern terminals)
- Optional:
tput(for better terminal detection, fallback to automatic detection)
- Go 1.25.4+ (for building from source)
- Terminal with 24-bit true color support (most modern terminals)
- No additional runtime dependencies
chmod +x matrix-rain.sh
./matrix-rain.shsudo cp matrix-rain.sh /usr/local/bin/matrix-rain
chmod +x /usr/local/bin/matrix-rain
matrix-rain # Run from anywhereRequirements: Go 1.25.4+
# Build for your current system
go build -o matrix-rain main.go
# Make executable and run
chmod +x matrix-rain
./matrix-rainUse the provided build script to compile for multiple architectures:
# Make build script executable
chmod +x build.sh
# Build all supported platforms
./build.sh
# Build with specific version
./build.sh 2.0.0
# Build with verbose output
./build.sh 2.0.0 -v
# Clean build directory and rebuild
./build.sh 2.0.0 -cSupported Platforms:
linux/amd64- Linux x86_64 PC (Intel/AMD processors)linux/arm64- Linux ARM64 (Android Termux, Raspberry Pi 4)linux/arm- Linux ARMv7 (Older Android devices, Raspberry Pi 3)darwin/amd64- macOS Intel (x86_64)darwin/arm64- macOS Apple Silicon (M1/M2)
Binaries will be in the build/ directory:
build/
├── matrix-rain-darwin-amd64 # macOS Intel
├── matrix-rain-darwin-arm64 # macOS Apple Silicon
├── matrix-rain-linux-x86_64 # Linux x86_64
├── matrix-rain-linux-arm64 # Linux ARM64 (Android Termux / Raspberry Pi 4)
└── matrix-rain-linux-arm32 # Linux ARMv7 (Android / Raspberry Pi 3)
If available, download pre-built binaries and make them executable:
chmod +x matrix-rain-linux-x86_64
./matrix-rain-linux-x86_64# Build first
go build -o matrix-rain main.go
# Install to PATH
sudo mv matrix-rain /usr/local/bin/
matrix-rain # Run from anywhere# From Linux or macOS, build for Android Termux (ARM64)
GOOS=linux GOARCH=arm64 go build -o matrix-rain-arm64 main.go
# Transfer to Android device via ADB or USB
adb push matrix-rain-arm64 /data/local/tmp/
adb shell chmod +x /data/local/tmp/matrix-rain-arm64
adb shell /data/local/tmp/matrix-rain-arm64./matrix-rain.sh [OPTIONS]./matrix-rain [OPTIONS]
# or if installed to PATH
matrix-rain [OPTIONS]Both implementations use the same command-line interface.
# Bash
./matrix-rain.sh
# Go
./matrix-rainStarts the animation with default settings (speed=5, density=80%).
./matrix-rain.sh [OPTIONS]
OPTIONS:
-s SPEED Animation speed (1-10, default: 5)
1 = very slow, 10 = very fast
-d DENSITY Column density (1-100%, default: 80)
Percentage of terminal width filled with columns
-h Show help message# Default animation
./matrix-rain.sh
# Fast animation with full density
./matrix-rain.sh -s 8 -d 100
# Slow animation with sparse columns
./matrix-rain.sh -s 2 -d 50
# Medium speed, medium density
./matrix-rain.sh -s 5 -d 70
# Minimal animation
./matrix-rain.sh -s 1 -d 10
# Maximum chaos
./matrix-rain.sh -s 10 -d 100| Key | Action |
|---|---|
Ctrl+C |
Stop animation and restore terminal |
| Terminal Resize | Animation automatically adapts to new dimensions |
| Feature | Bash | Go |
|---|---|---|
| Installation | No build required | Requires go build or pre-compiled binary |
| Performance | Moderate (pure bash) | Excellent (compiled binary) |
| Memory Usage | Higher (bash runtime) | Lower (native binary) |
| Startup Time | Slower (interpreter startup) | Instant (compiled binary) |
| Portability | Requires bash 4.0+ | Single binary, no runtime deps |
| Cross-Platform | Limited (bash availability) | Wide (easy cross-compilation) |
| Use Case | Learning, quick testing | Production, performance-critical |
| Android Termux | Works but slower | Optimized with native ARM binary |
| Development | Easy to modify | Requires Go knowledge |
TL;DR: Use Bash for immediate testing without setup. Use Go for performance and cross-platform deployment.
The implementations use a column-based approach where each falling stream operates independently:
- Terminal Setup - Switches to alternate screen buffer, hides cursor, detects dimensions
- Column Processes - Spawns background processes for each active column
- Animation Loop - Each column manages:
- Stream position and length
- Gap timing between streams
- Character trails with variable brightness
- Rainbow color calculation per position
- Rainbow Generation - Uses sine wave algorithm with 120° phase shifts for smooth RGB gradients
- Cleanup - Restores terminal on exit
Colors are generated using three phase-shifted sine waves:
red = sin(freq*position + 0°) * 127 + 128
green = sin(freq*position + 120°) * 127 + 128
blue = sin(freq*position + 240°) * 127 + 128
This creates a continuous, smooth rainbow gradient that cycles through all spectrum colors.
Each column maintains a history of characters with decreasing brightness:
- Head (current position): Bold, full intensity RGB color
- Trail (previous position): Normal weight, full intensity RGB color
- Fade (2+ positions back): Dim attribute, faded color
- Erase: Characters beyond trail length are removed
Edit the CHARS array in the script to use different characters:
# Example: Use ASCII characters instead
CHARS=( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" )
# Example: Mix ASCII and symbols
CHARS=( "█" "▓" "▒" "░" "|" "-" "/" "\\" "@" "#" "$" "%" )Change the rainbow gradient density by modifying the freq parameter in rainbow_color():
local freq=0.1 # Current: smooth gradient
local freq=0.05 # Slower color transitions
local freq=0.2 # Faster color transitionsTo adjust the delay calculation, edit the rain_column() function:
# Current formula (1-10 scale maps to ~0.1-0.9 seconds)
local delay=$(echo "scale=3; 1.0 - ($speed - 1) * 0.08" | bc -l)
# Make it faster overall:
local delay=$(echo "scale=3; 0.5 - ($speed - 1) * 0.04" | bc -l)
# Make it slower overall:
local delay=$(echo "scale=3; 1.5 - ($speed - 1) * 0.12" | bc -l)For terminals without 24-bit color support, you can implement a 256-color palette version by replacing ANSI color codes:
# 24-bit true color (current)
printf '\e[38;2;%d;%d;%dm' $r $g $b
# 256-color palette
printf '\e[38;5;%dm' $(color_to_256 $r $g $b)Language: Go 1.25.4+
Architecture: Native compiled binary
Key Differences from Bash:
- Concurrency: Uses goroutines for efficient frame rendering (one per animation frame cycle)
- Syscall Interface: Direct terminal control via
syscallpackage for IOCTL operations - No External Dependencies: Purely stdlib (no cmatrix or lolcat dependencies needed)
- Binary Size: ~4-6 MB per platform (self-contained, no runtime requirements)
- Performance:
- Bash: ~30-40% CPU on dense animations
- Go: ~5-10% CPU on same settings
- Memory Usage:
- Bash: 20-40 MB
- Go: 2-5 MB
Build Details:
- Single-pass compilation to native machine code
- Stripped binaries available with
go build -ldflags="-s -w" - Cross-compilation works from any platform where Go is installed
- Zero runtime dependencies (works on minimal systems)
| Sequence | Purpose |
|---|---|
\e[?1049h |
Switch to alternate screen buffer |
\e[2J |
Clear entire screen |
\e[?25l |
Hide cursor |
\e[?7l |
Disable line wrapping |
\e[L;CH |
Position cursor at line L, column C |
\e[1m |
Bold/bright intensity |
\e[2m |
Dim intensity |
\e[38;2;R;G;Bm |
Set foreground color to RGB |
\e[m |
Reset all attributes |
- Background Processes: Each column runs as a separate background process for parallelization
- Random Generation: Uses bash's built-in
$RANDOMfor character and speed selection - Minimal Redraws: Only updates changed positions instead of full screen refresh
- Sleep Timing: Uses fractional seconds for smooth animation control
Tested on:
- Linux (VT100, xterm, GNOME Terminal, Konsole, Alacritty)
- macOS (Terminal.app, iTerm2)
- WSL (Windows Subsystem for Linux)
Requires 24-bit color support (most terminals built after ~2018 have this).
# Reduce density to decrease CPU usage
./matrix-rain.sh -d 30
# Or reduce speed to lower processing requirements
./matrix-rain.sh -s 3# Maximum density and medium speed
./matrix-rain.sh -d 100 -s 6
# Or slower with full columns
./matrix-rain.sh -d 100 -s 3Issue: Animation shows up but without rainbow colors
Solutions:
- Check terminal supports 24-bit color:
echo $COLORTERM - Try setting color term manually:
export COLORTERM=truecolor - Try a different terminal (iTerm2, Alacritty, modern Konsole)
Issue: Animation isn't smooth
Solutions:
- Try reducing density:
./matrix-rain.sh -d 50 - Reduce terminal window size
- Close other CPU-intensive programs
- Try lower speed:
./matrix-rain.sh -s 3
Issue: Terminal state isn't properly restored
Solutions:
- Run
resetto restore terminal - Run
stty echoto re-enable terminal echo - The script should restore automatically on normal exit
Issue: Error about bc command not found
Solutions:
- Install bc:
sudo apt install bc(Ubuntu/Debian) orbrew install bc(macOS) - Or replace bc calls with pure bash arithmetic (less accurate)
# Maximum chaos - fill entire terminal with fast-moving columns
./matrix-rain.sh -s 9 -d 100# Slow, sparse animation perfect for a screensaver
./matrix-rain.sh -s 1 -d 30# Balanced speed and density for most users
./matrix-rain.sh# Keep animation in background while you work
./matrix-rain.sh -s 2 -d 20 &This script demonstrates that complex animations are possible in bash:
- No compiled dependencies required
- Portable across Unix-like systems
- Combines Unix tools (awk, printf) effectively
- Educational reference for bash scripting techniques
- Rainbow color algorithm: Based on lolcat (Ruby) implementation using sine waves
- Matrix rain effect: Inspired by cmatrix (C) with column-based streaming
- Terminal control: Uses standard ANSI escape sequences (VT100/xterm)
The build.sh script automates cross-platform compilation:
chmod +x build.sh
./build.sh # Build all platforms with defaults
./build.sh 2.0.0 # Build with version tag
./build.sh 2.0.0 -v # Verbose output
./build.sh 2.0.0 -c # Clean rebuild
./build.sh -h # Show help# Build for current platform
go build -o matrix-rain main.go
# Build with version info (optional)
go build -ldflags="-X main.version=2.0.0" -o matrix-rain main.go
# Create stripped binary (smaller size)
go build -ldflags="-s -w" -o matrix-rain main.go
# Build for specific platform
GOOS=linux GOARCH=arm64 go build -o matrix-rain-arm64 main.go- Build binaries for all target platforms
- Test binaries on x86_64 and ARM64 systems
- Verify terminal detection works (
./matrix-rain -h) - Test with different terminal sizes
- Verify Ctrl+C properly restores terminal
- Create release notes
- Upload binaries to releases page
To enhance this project:
- Character Sets: Add support for different character sets (Cyrillic, Greek, Arabic, etc.)
- Color Modes: Implement classic green-only Matrix mode
- Performance: Optimize for low-end systems
- Compatibility: Add 256-color palette fallback
- Features: Add mouse interaction or preset themes
- Go Optimization: Improve rendering performance or reduce binary size
Distributed as is under the MIT License
- cmatrix - Original C implementation of Matrix rain
- lolcat - Ruby tool for rainbow coloring terminal output
- figlet - ASCII art text generation (could combine with matrix-rain)
- Inspired by the visual effects from The Matrix (1999)
- Algorithm references from cmatrix and lolcat projects
- ANSI escape sequence documentation from various terminal emulator projects
Enjoy your digital rain! 🌧️🎌

