Skip to content

amber-lang/bash2amber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bash2amber

A transpiler that converts Bash scripts into Amber — a modern, type-safe language that compiles back to shell.

Installation

cargo install --path .

Usage

# Print Amber output to stdout
bash2amber script.sh

# Write Amber output to a file
bash2amber script.sh script.ab

What It Transforms

Variables and strings

name="World"
echo "Hello $name"
let name = "World"
echo("Hello {name}")

Arrays and loops

groceries=("apple" "banana" "cherry")
for item in "${groceries[@]}"; do
  echo "$item"
done
let groceries = ["apple", "banana", "cherry"]
for item in groceries {
    echo(item)
}

C-style for loops

for (( i=0; i<=5; i++ )); do
  echo "$i"
done
for i in 0..=5 {
    echo(i)
}

Conditionals

status="200"
if [ "$status" == "200" ]; then
  echo "UP"
else
  echo "DOWN"
fi
let status_var = "200"
if status_var == "200" {
    echo("UP")
} else {
    echo("DOWN")
}

Case statements

Case blocks are converted into if-chains.

fruit="pear"
case "$fruit" in
  apple|banana)
    echo "common"
    ;;
  pear)
    echo "pear"
    ;;
esac
let fruit = "pear"
if {
    fruit == "apple" or fruit == "banana" {
        echo("common")
    }
    fruit == "pear" {
        echo("pear")
    }
}

Arithmetic

base=10
result=$((base + 4 / 2))
echo "$result"
let base = 10
let result = base + 4 / 2
echo(result)

Functions

Annotate your Bash functions with ## fundoc comments to get typed Amber output.

## (msg: Text): Text(output)
wrap() {
  local msg="$1"
  output="[${msg}]"
}
wrap "hello"
echo $output
fun wrap(msg: Text): Text {
    return "[{msg}]"
}
let output = wrap("hello")
echo(output)

Ternary expressions

Simple if/else assignments are collapsed into ternary form.

mode="prod"
if [ "$mode" = "prod" ]; then
  status="live"
else
  status="test"
fi
echo "$status"
let mode = "prod"
let status_var = mode == "prod" then "live" else "test"
echo(status_var)

Unsupported constructs

When bash2amber encounters a construct it can't convert, it wraps the original shell code in a trust block so the output still compiles:

trust $ original-command $

About

Bash to Amber converter

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors