subst
parses its arguments as KEY=VALUE
pairs, substitutes $KEY
s by VALUE
s in stdin, and writes the result to stdout.
Using go get:
go get -u github.com/sgreben/subst
Or download the binary from the releases page.
# Linux
curl -LO https://github.com/sgreben/subst/releases/download/${VERSION}/subst_${VERSION}_linux_x86_64.zip
unzip subst_${VERSION}_linux_x86_64.zip
# OS X
curl -LO https://github.com/sgreben/subst/releases/download/${VERSION}/subst_${VERSION}_osx_x86_64.zip
unzip subst_${VERSION}_osx_x86_64.zip
# Windows
curl -LO https://github.com/sgreben/subst/releases/download/${VERSION}/subst_${VERSION}_windows_x86_64.zip
unzip subst_${VERSION}_windows_x86_64.zip
subst
uses key=value mappings given as CLI arguments, reads from stdin, and writes to stdout. The stdin input is line-buffered.
subst [OPTIONS] [KEY=VAL [KEY=VAL...]]
Usage of subst:
-q suppress all logs
-escape string
set the escape string. if set, a '$' preceded by the escape string does not lead to expansion
-unknown value
handling of unknown keys, one of [ignore empty error] (default ignore)
$ cat template.yaml
name: $NAME
url: $URL
comment: $COMMENT
$ cat template.yaml | subst -q NAME=jp URL=github.com/sgreben/jp COMMENT=
name: jp
url: github.com/sgreben/jp
comment:
$ cat template.yaml | subst -q NAME=jp
name: jp
url: $URL
comment: $COMMENT
$ cat template.yaml | subst -q -unknown=empty NAME=jp
name: jp
url:
comment:
Without the -q
flag, subst
also prints to stderr:
2018/04/16 15:18:37 undefined key: $URL, using value "$URL"
2018/04/16 15:18:37 undefined key: $COMMENT, using value "$COMMENT"
You can substitute environment variables (like envsubst
) by letting the shell substitute their values:
$ subst USER="$USER" SHELL="$SHELL"
$USER
sgreben
$SHELL
/bin/zsh
To ignore occurrences of defined variables, you can define an escape string using the -escape
flag:
$ echo 'unescaped: $X, ${X}. escaped: \$X, \${X}.' | subst -escape '\' X=value
unescaped: value, value. escaped: $X, ${X}.
Feel free to leave a comment or create an issue.