sh2lang v0.1.0

Initial public release of sh2lang, a structured shell language that compiles to Bash and POSIX sh.

This release establishes the core language syntax, execution model, built-in library, and dual-target code generation.


Highlights

  • Structured scripting with func, let, if, while, for
  • Safe command execution via run(...)
  • Bash and POSIX code generation (--target)
  • Expression-based logic and boolean model
  • Built-in filesystem, process, and string utilities
  • Try/catch error handling
  • Job control primitives (spawn, wait)
  • Pipe and redirection support

Core Language

Functions

func greet(name) {
    print("Hello " & name)
}
  • Parameters supported
  • Return statements supported
  • Functions callable as expressions or commands

Variables

  • let declaration
  • set reassignment
  • Boolean variables supported
  • Lists and maps supported

Control Flow

  • if / elif / else
  • while
  • for over:

    • Lists
    • Function args
  • break / continue
  • case pattern matching
  • try { ... } catch { ... }

Expressions

  • Arithmetic (+ - * /)
  • Boolean operators
  • Comparison operators
  • String concatenation with &
  • Unary operators
  • Parenthesized grouping

Command Model

run(…)

Execute external commands safely with automatic quoting.

Supports:

  • allow_fail=true

capture(…)

Capture stdout of a command.


sh(“…”)

Raw shell escape hatch.


Command Substitution

let out = capture(run("ls"))

Pipelines & Redirection

  • |
  • Redirect operators
  • Pipe blocks
  • Subshell and grouping support

Job Control

  • spawn(cmd)
  • wait(pid)
  • wait_all(list)
  • status()

Filesystem & Process Builtins

Filesystem

  • exists(path)
  • is_file(path)
  • is_dir(path)
  • is_nonempty(path)
  • chmod
  • cd(path)
  • source(path)

Process / Environment

  • pid()
  • uid()
  • ppid()
  • pwd()
  • argv0()
  • argc()
  • arg(n)
  • env.NAME
  • Export / unset

String & List Utilities

  • len(...)
  • split(string, delimiter)
  • join(list, sep)
  • index(list, i)
  • count(...)
  • contains(...)
  • contains_line(file, needle)
  • lines(...)
  • read_file(...)
  • File write builtin

Data Types

  • String
  • Boolean
  • Number
  • List
  • Map / Dictionary

Targets

Bash (default)

  • Uses Bash arrays
  • Supports advanced codegen features
  • Emits shebang for Bash

POSIX (--target posix)

  • Strict POSIX sh compatible
  • Quoting and portability contract enforced
  • POSIX lint integrated in CI

Tooling

sh2c

Compiler that converts .sh2 files to shell scripts.

Supports:

--target bash|posix
-o <file>

sh2do

Snippet runner:

sh2do 'print("hello")'

Supports snippet compilation and execution.


Stability Notes (v0.1.0)

  • Top-level statements banned (must be inside func main())
  • Strict quoting and escaping contract enforced
  • POSIX compatibility hardened
  • Diagnostics stabilized
  • Formatter included
  • VS Code syntax support included