Envsubst which supports parameter substution and expansion. Built for ARM and AMD architectures.
Latest stable envsubst
prebuilt binaries for 64-bit Linux, or MacOS
are available via GitHub releases.
sh <(curl "https://raw.githubusercontent.com/EdieLemoine/envsubst/main/install.sh") v1.4.0
This will download the given release for your current OS and architecture, untar it, make it executable and move it to /usr/local/bin/envsubst
.
To choose another destination, pass it as the second argument.
sh <(curl "https://raw.githubusercontent.com/EdieLemoine/envsubst/main/install.sh") v1.4.0 /path/to/envsubst
With uname
curl -L https://github.com/EdieLemoine/envsubst/releases/download/v1.4.0/envsubst-v1.4.0-`uname -s`-`uname -m` -o envsubst
chmod +x envsubst
sudo mv envsubst /usr/local/bin
Note: This is why the install script was created: On Linux, running
uname -m
on arm64 architecture will return 'aarch64', which will not work. The install script transforms this to 'arm64'.
Without uname
curl -L https://github.com/EdieLemoine/envsubst/releases/download/v1.4.0/envsubst-v1.4.0-linux-arm64 -o envsubst
chmod +x envsubst
sudo mv envsubst /usr/local/bin
Download the latest prebuilt binary from releases page, or if you have curl installed:
curl -L https://github.com/EdieLemoine/envsubst/releases/download/v1.4.0/envsubst.exe
You can install via go get
(provided you have installed go):
go get github.com/EdieLemoine/envsubst/cmd/envsubst
envsubst < input.tmpl > output.text
echo 'welcome $HOME ${USER:=a8m}' | envsubst
envsubst -help
There are three command line flags with which you can cause the substitution to stop with an error code, should the restriction associated with the flag not be met. This can be handy if you want to avoid creating e.g. configuration files with unset or empty parameters.
Setting a -fail-fast
flag in conjunction with either no-unset or no-empty or
both will result in a faster feedback loop, this can be especially useful when
running through a large file or byte array input, otherwise a list of errors is
returned.
The flags and their restrictions are:
Option | Meaning | Type | Default |
---|---|---|---|
-i |
input file | string / stdin |
stdin |
-o |
output file | string / stdout |
stdout |
-no-unset |
fail if a variable is not set | flag |
false |
-no-empty |
fail if a variable is set but empty | flag |
false |
-fail-fast |
fails at first occurence of an error, if -no-empty or -no-unset flags were not specified this is ignored |
flag |
false |
These flags can be combined to form tighter restrictions.
You can take a look
on _example/main
or see the example below.
package main
import (
"fmt"
"github.com/EdieLemoine/envsubst"
)
func main() {
input := "welcom $HOME"
str, err := envsubst.String(input)
// ...
buf, err := envsubst.Bytes([]byte(input))
// ...
buf, err := envsubst.ReadFile("filename")
}
Expression | Meaning |
---|---|
${var} |
Value of var (same as $var ) |
${var-$DEFAULT} |
If var not set, evaluate expression as $DEFAULT |
${var:-$DEFAULT} |
If var not set or is empty, evaluate expression as $DEFAULT |
${var=$DEFAULT} |
If var not set, evaluate expression as $DEFAULT |
${var:=$DEFAULT} |
If var not set or is empty, evaluate expression as $DEFAULT |
${var+$OTHER} |
If var set, evaluate expression as $OTHER, otherwise as empty string |
${var:+$OTHER} |
If var set, evaluate expression as $OTHER, otherwise as empty string |
$$var |
Escape expressions. Result will be $var . |
Most of the rows in this table were taken from here
os.ExpandEnv(s string) string
- only supports$var
and${var}
notations
MIT
This was originally made by a8m. I just forked it to add arm64 binaries. I'm willing to maintain this, though, so pull requests are welcome.