Skip to content

Instantly share code, notes, and snippets.

@gordcurrie
Last active December 12, 2020 18:52
Show Gist options
  • Select an option

  • Save gordcurrie/465053da9b17f031ef6b62df33312ff5 to your computer and use it in GitHub Desktop.

Select an option

Save gordcurrie/465053da9b17f031ef6b62df33312ff5 to your computer and use it in GitHub Desktop.
Using envsubst to generate code.
#!/bin/bash
# if called with no params sets the value of $1 to "World"
if [ -z $1 ]
then
set "World"
fi
# sets the env var used in template
export yourName=$1 \
attribute="cool"
# subsitues the variables in the template with the env vars
# and outputs to the new file
envsubst < ./hello.go.tmpl > ./hello${1}.go
package main
import "fmt"
func main() {
fmt.Println("Hello ${yourName}! You are ${attribute}.")
}
package main
import "fmt"
func main() {
fmt.Println("Hello Gord! You are cool.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment