File size: 3,047 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | # envsubst — Usage Documentation
Environment variables substitution tool (Go implementation of shell-style variable expansion).
## Installation
### From prebuilt binaries
Latest stable `envsubst` binaries for 64-bit Linux, macOS, and Windows are available via the GitHub releases page.
Linux / macOS:
```console
curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst
chmod +x envsubst
sudo mv envsubst /usr/local/bin
```
Windows:
```console
curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst.exe
```
### With Go
```console
go get github.com/a8m/envsubst/cmd/envsubst
```
## CLI usage
```sh
envsubst < input.tmpl > output.text
echo 'welcome $HOME ${USER:=a8m}' | envsubst
envsubst -help
```
If no input file is given, input is read from stdin. If no output file is given, output is written to stdout.
## Command-line flags
| Option | Meaning | Type | Default |
| --- | --- | --- | --- |
| `-i` | input file | `string \| stdin` | `stdin` |
| `-o` | output file | `string \| stdout` | `stdout` |
| `-no-digit` | do not replace variables starting with a digit, e.g. `$1` and `${1}` | flag | `false` |
| `-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` | fail on first error instead of collecting all errors; ignored unless `-no-empty` or `-no-unset` is set | flag | `false` |
These flags can be combined to form tighter restrictions (e.g. to avoid creating configuration files with unset or empty parameters). Setting `-fail-fast` together with `-no-unset` and/or `-no-empty` gives a faster feedback loop on large inputs; otherwise a list of all errors is returned.
## Supported expressions
| 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: result will be `$var` |
Note: `os.ExpandEnv` only supports the `$var` and `${var}` notations; this tool supports the full set above.
## Programmatic (Go API) usage
```go
package main
import (
"fmt"
"github.com/a8m/envsubst"
)
func main() {
input := "welcome $HOME"
str, err := envsubst.String(input)
// ...
buf, err := envsubst.Bytes([]byte(input))
// ...
buf, err := envsubst.ReadFile("filename")
}
```
## Example input/output
`assets/config.yaml` shows a configuration template using substitution forms. `assets/file.tmpl` is a sample template and `assets/file.out` its expected output after substitution.
## License
MIT
|