Go/Golang Run embedded bash script or node/js,python,php,ruby etc

Since Go v1.16 there’s the embed package, a simple tool to embed files as filesystems of simple strings or []bytes.

While it has its downsides, I was recently met with a challenge. I’d like to run bash scripts I wrote because it’s more efficient to just run a bash script than breaking my fingers writing os/exec stuff.

Anyhow it’s pretty simple really. Any shell command has a standard input and output. You just assign the file pointer (aka the Reader) to the os.Command’s Stdin.

Example:
Imagine the following directory structure

bash/test.bash

install.go

Output:
Hello Sunny, this is a test

Imagine the possibilities. You can now embed PHP, JS, all kinds of scripting languages in Go and execute those scripts without ever needing to write any of the embedded content to disk. Of course you need the interpreters installed, but if you have enough time on your hands maybe you could figure out how to embed a scripting language and virtually pipe a script to an interpreter.

And while that’s nice, I do have a problem. How do you pass arguments like this?
Thanks to Jon Lundy this problem is solved.

bash/hello.bash

and
main.go

Output:
Hello there,
Jon
Lundy

As an alternative you can also use

instead of

2 Replies to “Go/Golang Run embedded bash script or node/js,python,php,ruby etc”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.