Command-line interface for environment variable substitution in text files.
The EnvSubstCommand provides a CLI wrapper around the EnvSubst module, allowing users to substitute environment variables in text files or stdin using shell-style variable expansion. It supports various substitution patterns including default values, alternate values, and error handling.
mpct envsubst [options]
mpct envsubst --input <file> --output <file> [options]
echo 'Hello $USER' | mpct envsubstThe command supports the following shell-style variable expansion patterns:
$varor${var}- Value of var${var-default}- Use default if var not set${var:-default}- Use default if var not set or empty${var+alternate}- Use alternate if var is set${var:+alternate}- Use alternate if var is set and not empty$$var- Escape to literal $var
| Argument | Required | Description |
|---|---|---|
--input, -i |
No | Input file path. If not specified, reads from stdin. |
--output, -o |
No | Output file path. If not specified, writes to stdout. |
--no-unset |
No | Fail if a referenced variable is not set in the environment. By default, unset variables are replaced with empty strings. |
--no-empty |
No | Fail if a referenced variable is set but contains an empty value. By default, empty variables are allowed and their empty values are used. |
--fail-fast |
No | Stop processing at the first error encountered during substitution. By default, processing continues and errors are collected for reporting at the end. |
Substitute variables in a string:
echo 'Hello $USER from $HOME' | mpct envsubst
# Output: Hello john from /Users/johnProcess a template file and save the result:
mpct envsubst --input template.txt --output config.txtTemplate with default values:
echo 'Database: ${DB_HOST:-localhost}:${DB_PORT:-5432}' | mpct envsubst
# Output: Database: localhost:5432 (if DB_HOST and DB_PORT are not set)Template with alternate values:
echo 'Mode: ${DEBUG+development}${DEBUG:+debug-enabled}' | mpct envsubst
# Output depends on whether DEBUG is set and its valueFail on unset or empty variables:
mpct envsubst --no-unset --no-empty < template.txtStop at first error:
mpct envsubst --fail-fast --no-unset < template.txt# Input: Hello $USER
# Environment: USER=alice
# Output: Hello alice# Input: Server: ${SERVER:-localhost}
# Environment: (SERVER not set)
# Output: Server: localhost
# Input: Server: ${SERVER:-localhost}
# Environment: SERVER=production.example.com
# Output: Server: production.example.com# Input: Port: ${PORT:-8080}
# Environment: PORT=""
# Output: Port: (empty string)
# Input: Port: ${PORT:-8080}
# Environment: PORT=""
# Output: Port: 8080# Input: ${DEBUG+Debug mode enabled}
# Environment: DEBUG=1
# Output: Debug mode enabled
# Input: ${DEBUG+Debug mode enabled}
# Environment: (DEBUG not set)
# Output: (empty)# Input: Price: $$10.99
# Output: Price: $10.99The command handles various error conditions:
- Default behavior: Replace with empty string
- With
--no-unset: Fail with error message
- Default behavior: Use empty value
- With
--no-empty: Fail with error message
- Default behavior: Collect all errors and report at the end
- With
--fail-fast: Stop at first error
Generate configuration files from templates:
mpct envsubst --input config.template --output config.jsonProcess Docker compose templates:
mpct envsubst --input docker-compose.template.yml --output docker-compose.ymlGenerate pipeline configurations with environment-specific values:
mpct envsubst --input .github/workflows/template.yml --output .github/workflows/deploy.ymlCreate application config from environment variables:
mpct envsubst --input app-config.template --output app-config.json --no-unsetThe EnvSubstCommand is also used internally by other commands:
- ExportSecretsCommand: Uses envsubst options for processing configuration files
- ObfuscateSecretsCommand: Uses envsubst options for variable substitution in configuration
Common error scenarios and their messages:
- File not found: "Failed to read input file 'filename': No such file or directory"
- Permission denied: "Failed to write output file 'filename': Permission denied"
- Unset variable: "Variable 'VAR_NAME' is not set" (with
--no-unset) - Empty variable: "Variable 'VAR_NAME' is empty" (with
--no-empty) - Substitution error: "Substitution failed: [specific error message]"
- Processing is done in memory, so very large files may consume significant RAM
- File I/O is performed atomically for output files
- Environment variable lookups are cached for performance
- Be cautious when processing untrusted input files
- Ensure proper file permissions on output files containing sensitive data
- Consider the security implications of environment variable expansion
- Validate that sensitive variables are properly set before processing
import- For importing secrets with environment variable substitutionobfuscate- For obfuscating secrets with environment variable substitution
- Read permissions for input files
- Write permissions for output files (when specified)
- Access to environment variables referenced in templates