diff --git a/cmd/tengo/main.go b/cmd/tengo/main.go index c73973ba..f67e2a87 100644 --- a/cmd/tengo/main.go +++ b/cmd/tengo/main.go @@ -67,6 +67,10 @@ func main() { os.Exit(1) } } else if filepath.Ext(inputFile) == sourceFileExt { + if len(inputData) > 1 && + bytes.Compare(inputData[:2], []byte("#!")) == 0 { + copy(inputData, "//") + } err := CompileAndRun(modules, inputData, inputFile) if err != nil { _, _ = fmt.Fprintln(os.Stderr, err.Error()) diff --git a/docs/tengo-cli.md b/docs/tengo-cli.md index 0f8c98c3..2e7179e1 100644 --- a/docs/tengo-cli.md +++ b/docs/tengo-cli.md @@ -30,6 +30,28 @@ tengo -o myapp myapp.tengo # compile 'myapp.tengo' into binary file 'myapp' tengo myapp # execute the compiled binary `myapp` ``` +Or, you can make tengo source file executable + +```bash +# copy tengo executable to a dir where PATH environment variable includes +cp tengo /usr/local/bin/ + +# add shebang line to source file +cat > myapp.tengo << EOF +#!/usr/local/bin/tengo +fmt := import("fmt") +fmt.println("Hello World!") +EOF + +# make myapp.tengo file executable +chmod +x myapp.tengo + +# run your script +./myapp.tengo +``` + +**Note: Your source file must have `.tengo` extension.** + ## Tengo REPL You can run Tengo [REPL](https://en.wikipedia.org/wiki/Read–eval–print_loop)