#env-var #variables #environment #cli #line #command-line #envsubst

app envsub

Command line utility for substituion of environment variables

2 releases

0.1.3 Nov 25, 2020
0.1.2 Nov 25, 2020

#2826 in Command line utilities

Download history 1/week @ 2023-11-18 7/week @ 2024-01-06 9/week @ 2024-01-20 22/week @ 2024-01-27 8/week @ 2024-02-03 15/week @ 2024-02-10 28/week @ 2024-02-17 41/week @ 2024-02-24 5/week @ 2024-03-02

91 downloads per month

Apache-2.0

12KB
203 lines

envsub

$ envsub [options]

The envsub program substitutes the values of environment variables. Unlike envsubst, which only supports ${NAME} style substitution, the envsub program also supports ${NAME-default} and ${NAME:-default} style substitution.

The program pipes stdin to stdout one line at a time applying the required substitutions.

Command line options

-h, --help:: Displays the help details of the command and exit.

-p, --prefix:: Allows configuring an alternative prefix for substitution patterns if the default of ${ would cause conflicts.

-s, --suffix:: Allows configuring an alternative suffix for substitution patterns if the default of } would cause conflicts.

NOTE: If your suffix starts with : or - you will not have a good time as this will conflict with the default value separator that is internal to the pattern.

-g, --greedy-defaults:: Enables greedy replacement of unmatched default values. With this option if you have ${FOO} and there is no corresponding variable FOO then that will remain untouched but ${FOO:-BAR} or ${FOO-BAR} will be replaced as BAR whereas without this option they would remain untouched.

-v, --var:: Only replace the named variable. Can be specified multiple times if you want to replace multiple variables. If not specified then all variables from the environment can be substituted.

-V, --version:: Display the program version and exit.

Examples

TIP: When running envsub without the -v argument then only patterns that have environment variables will be searched for, so the ${NAME-default} form will never be replaced if NAME is not defined.

Basic usage:

$ (echo 'FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}') | (unset FOO && envsub)
FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}
$ (echo 'FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}') | (unset FOO && envsub -v FOO)
FOO=${FOO} or unset or empty
$ (echo 'FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}') | (FOO= envsub)
FOO= or  or empty
$ (echo 'FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}') | (FOO= envsub -v BAR)
FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}
$ (echo 'FOO=${FOO} or ${FOO-unset} or ${FOO:-empty}') | (FOO=123 envsub)
FOO=123 or 123 or 123

Custom prefixes and suffixes:

$ (echo 'FOO=%FOO:-empty%') | (FOO= envsub -p '%' -s '%')
FOO=empty

Dependencies

~2.3–3.5MB
~56K SLoC