← Back to Home

Table of Contents

Architecture Overview

Gosh is organized into multiple modules for maximum readability and maintainability:

Advanced Variable Expansion

Variable Declaration Requirements

All variables in gosh must be explicitly declared with either local or export:

# Local variables (current shell only)
local TEMP_VAR="temporary"
local COUNT=42

# Environment variables (exported to child processes)
export PATH="$PATH:/new/path"
export CONFIG_FILE="app.conf"

Complex Variable Substitution

# Nested variable expansion
export BASE_DIR="/home/user"  # Environment variable
export PROJECT_DIR="${BASE_DIR}/projects"  # Environment variable
local CURRENT_PROJECT="${PROJECT_DIR}/gosh"  # Local variable

echo "Working in: $CURRENT_PROJECT"

Variable Expansion in Command Substitution

export SEARCH_TERM="error"  # Environment variable
export LOG_FILE="/var/log/system.log"  # Environment variable

# Use variables within command substitution
local OCCURRENCE_COUNT="$(grep "$SEARCH_TERM" "$LOG_FILE" | wc -l)"  # Local variable
echo "Found $OCCURRENCE_COUNT occurrences"

Line Continuation

Gosh supports line continuation using backslashes (\) at the end of lines, allowing you to split long commands across multiple lines for better readability.

Basic Line Continuation

# Split a long echo command
echo "This is a very long message" \
  "that spans multiple lines" \
  "for better readability"

# Long command with many arguments
ls -la \
  *.go \
  *.md \
  *.txt

Line Continuation in Pipelines

# Multi-stage pipeline
cat large_file.txt | \
  grep "pattern" | \
  sort | \
  uniq -c | \
  head -20

Line Continuation in Command Chains

# Sequential commands
echo "Step 1: Preparing..."; \
  mkdir -p output; \
  echo "Step 2: Processing..."; \
  touch output/result.txt; \
  echo "Step 3: Complete!"

Windows Path Separator Compatibility

Gosh intelligently distinguishes between line continuation backslashes and Windows path separators:

# These work correctly on Windows (no line continuation)
cd C:\Users\username\Documents
cd ~\Projects\
ls *.txt | grep pattern > results.txt

# These are actual line continuations
echo "This is a long command" \
  "that continues on the next line"
ls -la \
  *.go \
  *.md

Interactive vs Script Usage

Line continuation works in both interactive mode and script files:

Best Practices

Advanced Glob Patterns

Character Classes

# Match files starting with uppercase letters
ls [A-Z]*

# Match files with specific extensions
ls *.[ch]  # .c or .h files

# Exclude patterns (when supported by underlying system)
ls !(*.tmp)  # All files except .tmp files

Complex Glob Combinations

# Multiple patterns
ls *.{go,md,txt}

# Nested directory patterns
find . -name "*.go" -type f

Advanced Piping Techniques

Multi-stage Data Processing

# Complex data pipeline with line continuation
cat data.csv | \
  grep -v "^#" | \
  cut -d',' -f2,3 | \
  sort | \
  uniq -c | \
  sort -nr | \
  head -10

Combining Pipes with Redirection

# Process data and save both intermediate and final results
cat input.txt | \
  tee intermediate.txt | \
  sort | \
  tee sorted.txt | \
  uniq > final.txt

Environment Management

Advanced Environment Variable Techniques

# Save and restore environment
local OLD_PATH="$PATH"  # Local variable to store original PATH
export PATH="/custom/bin:$PATH"  # Environment variable
# ... do work with custom PATH ...
export PATH="$OLD_PATH"  # Restore original PATH
unset OLD_PATH

Dynamic Environment Setup

# Set environment based on conditions
export ENVIRONMENT="development"  # Environment variable
export CONFIG_FILE="config_${ENVIRONMENT}.json"  # Environment variable
export LOG_LEVEL="debug"  # Environment variable

echo "Using config: $CONFIG_FILE"
echo "Log level: $LOG_LEVEL"

Advanced Aliasing

Complex Aliases

# Aliases with multiple commands
alias backup="tar -czf backup_$(date +%Y%m%d).tar.gz"
alias logcheck="tail -f /var/log/system.log | grep ERROR"

# Aliases with pipes
alias psgrep="ps aux | grep"
alias netstat="netstat -tuln | grep LISTEN"

Temporary Aliases

# Create temporary aliases for a session
alias temp_alias="echo 'This is temporary'"
temp_alias
unalias temp_alias

Advanced Scripting Patterns

Error Handling Patterns

#!/usr/bin/env gosh

# Function-like command grouping
echo "Starting backup process..."

# Check if source directory exists
if [ -d "$SOURCE_DIR" ]; then
    echo "Source directory found: $SOURCE_DIR"
else
    echo "Error: Source directory not found: $SOURCE_DIR"
    exit 1
fi

# Perform backup with status checking
tar -czf "backup.tar.gz" "$SOURCE_DIR"
echo "Backup completed with status: $?"

Configuration Management

#!/usr/bin/env gosh
# Configuration-driven script

# Load configuration
export CONFIG_FILE="${HOME}/.gosh_config"
if [ -f "$CONFIG_FILE" ]; then
    echo "Loading configuration from $CONFIG_FILE"
else
    echo "Using default configuration"
    export DEFAULT_EDITOR="nano"
    export DEFAULT_PAGER="less"
fi

Performance Optimization

Efficient Command Patterns

# Avoid unnecessary command substitution
# Instead of: echo "Files: $(ls | wc -l)"
# Use when possible: ls | wc -l

# Minimize pipe stages
# Instead of: cat file | grep pattern | wc -l
# Use: grep pattern file | wc -l

Memory-Efficient Processing

# Process large files efficiently
# Stream processing instead of loading entire files
tail -f large_log.txt | grep "ERROR" | head -100

# Use appropriate tools for the job
grep "pattern" large_file.txt > matches.txt  # Better than cat | grep

System Integration

# System monitoring scripts
echo "System Status Report - $(date)"
echo "=================================="
echo "Disk Usage:"
df -h | head -5
echo ""
echo "Memory Usage:"
free -h 2>/dev/null || vm_stat
echo ""
echo "Load Average:"
uptime

Debugging and Troubleshooting

Debug Mode Techniques

#!/usr/bin/env gosh
# Enable debug output
export DEBUG=1

if [ "$DEBUG" = "1" ]; then
    echo "Debug: Starting script execution"
fi

# Trace command execution
echo "Executing: ls -la"
ls -la

if [ "$DEBUG" = "1" ]; then
    echo "Debug: Command completed"
fi

Error Logging

# Log errors to file
export ERROR_LOG="/tmp/gosh_errors.log"

# Redirect errors
command_that_might_fail 2>> "$ERROR_LOG"

# Check for errors
if [ -s "$ERROR_LOG" ]; then
    echo "Errors occurred, check $ERROR_LOG"
fi

Best Practices for Advanced Usage

  1. Modular Scripts: Break complex scripts into smaller, reusable components
  2. Error Handling: Always consider what happens when commands fail
  3. Resource Management: Be mindful of memory and CPU usage in scripts
  4. Security: Never expose sensitive data in command lines or environment variables
  5. Portability: Write scripts that work across different systems when possible
  6. Documentation: Comment complex logic and provide usage examples
  7. Testing: Test scripts with various inputs and edge cases

Platform-Specific Considerations

macOS Compatibility

Gosh is designed and tested for macOS, taking advantage of:

Cross-Platform Scripting

# Detect operating system
if command -v uname >/dev/null 2>&1; then
    export OS_TYPE=$(uname -s)
else
    export OS_TYPE="Windows"
fi
case "$OS_TYPE" in
    "Darwin")
        echo "Running on macOS"
        export OPEN_CMD="open"
        ;;
    "Linux")
        echo "Running on Linux"
        export OPEN_CMD="xdg-open"
        ;;
    *)
        echo "Unknown operating system: $OS_TYPE"
        ;;
esac

Future Enhancements

Areas for potential future development:

Gosh continues to evolve while maintaining its core philosophy of simplicity and reliability!