✨ Rush Shell Features

Comprehensive POSIX sh-compatible shell with advanced expansions, powerful built-ins, and modern interface

🔧 Core Features

Command Execution

Execute external commands and built-in commands with full PATH resolution and error handling

# External commands
ls -la /tmp
curl https://api.example.com

# Built-in commands
cd /home/user
pwd
env
🔗

Pipelines

Chain commands using the pipe operator for powerful data processing workflows

# Basic pipelines
ls | grep ".txt" | wc -l
cat file.txt | sort | uniq

# Complex pipelines
find . -name "*.rs" | xargs grep -l "fn main" | head -5
📝

Redirections

Input and output redirections for flexible I/O handling in commands and scripts

# Output redirection
echo "Hello World" > hello.txt
ls -la >> log.txt

# Input redirection
sort < unsorted.txt > sorted.txt
grep "pattern" < file.txt
📄

Here-Documents & Here-Strings

Multi-line and single-line input redirection with full variable expansion support

# Here-document (multi-line)
cat << EOF
Line 1: $USER
Line 2: $HOME
EOF

# Here-string (single-line)
grep "pattern" <<< "search this text"
wc -w <<< "count these words"
🔀

File Descriptor Operations

Complete file descriptor management with duplication, closing, and read/write operations

# Duplicate file descriptors
exec 3>&1  # Duplicate stdout to FD 3
exec 4<&0  # Duplicate stdin to FD 4

# Close file descriptors
exec 3>&-  # Close FD 3
exec 4<&-  # Close FD 4

# Read/write operations
exec 5<>file.txt  # Open for read/write
🔄

Subshells

POSIX-compliant subshells with state isolation, exit code propagation, and trap inheritance

# Execute in subshell
(cd /tmp && ls)
# Current directory unchanged

# Subshell with variables
(VAR=value; echo $VAR)
# VAR not set in parent shell

# Nested subshells
((echo "nested"))  # Depth limit: 100
📦

Command Grouping

Group commands in the current shell context using braces for shared state and redirections

# Group commands with shared state
{ x=1; echo $x; }
echo $x  # x is still 1 (unlike subshells)

# Grouping with redirection
{
  echo "Start"
  date
} > output.log

🔄 Expansions

Brace Expansion

Generate multiple strings from patterns with braces - perfect for batch operations and file generation

Basic Lists

echo {a,b,c}
# Output: a b c

echo file{1,2,3}.txt
# Output: file1.txt file2.txt file3.txt

Numeric Ranges

echo {1..5}
# Output: 1 2 3 4 5

echo test{1..3}.log
# Output: test1.log test2.log test3.log

Alphabetic Ranges

echo {a..e}
# Output: a b c d e

echo file{a..c}.txt
# Output: filea.txt fileb.txt filec.txt

Real-World Usage

# Create multiple directories
mkdir -p project/{src,test,docs}

# Create numbered backup files
cp important.txt important.txt.{1..5}

# Generate test data
echo user{1..100}@example.com

Command Substitution

Execute commands and substitute their output inline within the current shell context for optimal performance

Basic Substitution

echo "Current dir: $(pwd)"
echo "Files: `ls | wc -l`"

# Variable assignments
PROJECT_DIR="$(pwd)/src"
FILE_COUNT="$(ls *.rs 2>/dev/null | wc -l)"

Advanced Usage

# Complex commands
echo "Rust version: $(rustc --version | cut -d' ' -f2)"

# Error handling
RESULT="$(nonexistent_command 2>/dev/null || echo 'failed')"

# Multiple commands
echo "Output: $(echo 'First'; echo 'Second')"

Arithmetic Expansion

Evaluate mathematical expressions using POSIX-standard $((...)) syntax with full operator precedence

Basic Arithmetic

echo "Result: $((2 + 3 * 4))"
echo "Division: $((20 / 4))"
echo "Modulo: $((17 % 3))"

Variable Integration

x=10
y=3
echo "x + y = $((x + y))"
echo "x squared = $((x * x))"

Complex Expressions

# With precedence
echo "2 + 3 * 4 = $((2 + 3 * 4))"        # 14
echo "(2 + 3) * 4 = $(((2 + 3) * 4))"    # 20

# Temperature conversion
celsius=25
fahrenheit=$((celsius * 9 / 5 + 32))

Parameter Expansion with Modifiers

Advanced variable expansion with POSIX sh modifiers for powerful string manipulation. Supports both $VAR and ${VAR} brace syntax for all variable types including special variables like $LINENO.

Variable Expansion Syntax

# Both syntaxes supported
echo $HOME
echo ${HOME}

# Works with special variables
echo $LINENO
echo ${LINENO}

# PS4 variable expansion for xtrace
PS4='+ ${LINENO}: '
set -x
echo "Debug output"

Default Values

# Use default if unset or null
echo "Home: ${HOME:-/home/user}"
echo "Editor: ${EDITOR:-vim}"

# Assign default if unset
echo "Editor: ${EDITOR:=nano}"

Substring Operations

FILENAME="document.txt"
echo "Extension: ${FILENAME:9}"           # "txt"
echo "Name only: ${FILENAME:0:8}"         # "document"
echo "Length: ${#FILENAME}"               # "13"

Pattern Operations

# Remove file extensions
echo "No extension: ${FILENAME%.txt}"

# Remove directory paths
FULL_PATH="/usr/bin/ls"
echo "Basename: ${FULL_PATH##*/}"         # "ls"

# Pattern substitution
echo "Replaced: ${TEXT/old/new}"

Indirect Expansion (bash extension)

# Basic indirect expansion
VAR_NAME="MESSAGE"
MESSAGE="Hello World"
echo "Value: ${!VAR_NAME}"                # "Hello World"

# List variables by prefix
MY_VAR1="a"
MY_VAR2="b"
echo "All MY_ vars: ${!MY_*}"             # "MY_VAR1 MY_VAR2"

🎮 Control Structures

Conditional Statements

If Statements

if [ -f "/etc/passwd" ]; then
    echo "File exists"
elif [ -d "/etc" ]; then
    echo "Directory exists"
else
    echo "Neither exists"
fi

Case Statements

case $filename in
    *.txt|*.md) echo "Text file" ;;
    *.jpg|*.png) echo "Image file" ;;
    file?) echo "Single character file" ;;
    [abc]*) echo "Starts with a, b, or c" ;;
    *) echo "Other file type" ;;
esac

Loops

For Loops

for i in 1 2 3 4 5; do
    echo "Number: $i"
done

for file in *.txt; do
    echo "Processing: $file"
    wc -l "$file"
done

While Loops

count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    count=$((count + 1))
done

Until Loops

count=1
until [ $count -gt 5 ]; do
    echo "Count: $count"
    count=$((count + 1))
done

Loop Control

# Break - exit from loop
for i in 1 2 3 4 5; do
    if [ $i -eq 3 ]; then
        break  # Exit loop when i equals 3
    fi
    echo "Number: $i"
done

# Continue - skip to next iteration
for i in 1 2 3 4 5; do
    if [ $i -eq 3 ]; then
        continue  # Skip 3
    fi
    echo "Number: $i"
done

# Nested loops with break [n]
for i in 1 2 3; do
    for j in a b c; do
        if [ "$j" = "b" ]; then
            break 2  # Break out of both loops
        fi
        echo "$i-$j"
    done
done

Functions

Function Definition

myfunc() {
    echo "Hello $1!"
    local result=$(( $2 + $3 ))
    return $result
}

# Call function
myfunc "World" 2 3
echo "Exit code: $?"

Function Introspection

# List all functions
declare -f

# Show specific function
declare -f myfunc

# Local variables
local var="value"

🛠️ Built-in Commands

Rush includes 32 comprehensive built-in commands for enhanced functionality and performance:

Process Management

  • cd - Change directory
  • exit - Exit the shell
  • pwd - Print working directory

Variable Management

  • env - List environment variables
  • export - Export variables to child processes
  • unset - Remove variables
  • shift - Shift positional parameters

Shell Options

  • set - Set shell options and positional parameters

Directory Stack

  • pushd - Push directory and change
  • popd - Pop directory and change
  • dirs - Display directory stack

Alias Management

  • alias - Define or display aliases
  • unalias - Remove alias definitions

Script Execution

  • source / . - Execute script files
  • test / [ - Conditional logic tests

Signal Handling

  • trap - Set signal handlers and traps

Loop Control

  • break - Exit from loops
  • continue - Skip to next loop iteration
  • return - Return from functions

Job Control

  • bg - Resume jobs in background
  • fg - Bring jobs to foreground
  • jobs - List active jobs
  • kill - Send signals to jobs
  • wait - Wait for job completion

Interface & Introspection

  • help - Show available commands
  • set_colors - Control color output
  • set_color_scheme - Switch themes
  • declare - Function introspection
  • type - Display information about command type (alias, keyword, function, builtin, or external command)

Utility

  • : - Null command (no-op), always returns success
  • times - Display shell and child process CPU times

🚀 Advanced Features

🎨

Color Support

Modern terminal interface with customizable color schemes and accessibility support

# Runtime color control
set_colors on
set_color_scheme dark

# Accessibility support
export NO_COLOR=1
📁

Tab Completion

Intelligent completion for commands, files, directories, and nested paths

# Command completion
cd → cd, env, exit

# File completion
cat f → cat file.txt

# Path completion
ls src/m → ls src/main/
⚙️

Configuration

Automatic sourcing of ~/.rushrc for personalized shell environment

# ~/.rushrc
export EDITOR=vim
alias ll='ls -la'
echo "Welcome to Rush!"
🛡️

Enhanced Trap System

Signal normalization, multiple handlers, trap display/reset, and signal queue with overflow protection

# Set trap handlers
trap 'echo "Interrupted"' INT
trap 'cleanup' EXIT

# Display traps
trap

# Reset trap
trap - INT