bash2go

command module
v0.0.0-...-9234c01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 3 Imported by: 0

README

bash2go

A Bash-to-Go Transpiler that converts Bash scripts to idiomatic Go code.

Overview

bash2go is a tool that translates Bash scripts into Go programs, producing compiled binaries that replace shell scripts. This provides better performance, portability, and maintainability than traditional shell scripts.

Features

  • Converts Bash scripts to idiomatic Go code
  • Handles common Bash constructs:
    • Variable assignments and substitutions
    • Command execution
    • Control flow (if, for, while, until, case)
    • Functions
    • Pipes and redirections
    • Subshells and background tasks
  • Generates standalone Go executables

Installation

go install github.com/TFMV/bash2go@latest

Usage

Converting a Bash script to Go
bash2go convert script.sh -o script.go
Building a Bash script directly to a binary
bash2go build script.sh -o script

Examples

Simple Hello World

hello.sh:

#!/bin/bash
echo "Hello, World!"

Generated Go code:

package main

import (
    "fmt"
)

// Main function generated from Bash script
func main() {
    fmt.Println("Hello, World!")
}
More Complex Example

example.sh:

#!/bin/bash
NAME="World"
echo "Hello, $NAME!"

if [ -f "file.txt" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi

for i in 1 2 3; do
    echo "Item $i"
done

Project Structure

  • cmd/: Command-line interface
  • parser/: Bash script parsing and AST building
  • generator/: Go code generation
  • compiler/: Go code compilation
  • examples/: Example Bash scripts and their Go equivalents

Development

Building from source
git clone https://github.com/TFMV/bash2go.git
cd bash2go
go build
Running tests
go test ./...

Limitations

  • Not all Bash features are supported yet
  • Complex shell expansions may not translate perfectly
  • External command execution relies on the gexe library

License

MIT License

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
generator.go
generator.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL