Variable substitution in .env files
masterYou can reuse variables within your .env file using $VARIABLE or ${VARIABLE} syntax, similar to bash.
Substitution Rules:
- Non-existing values: Replaced with an empty string.
- Variable Names: All letters following the
$symbol are treated as the variable name. For non-alphanumeric names, use curly braces${VAR_NAME}. - Quotes: Double quotes (
"$VAR") do not prevent substitution. - Escaping: To prevent substitution, use single quotes (
'$VAR') or a backslash (\$VAR). - Precedence: Environment variables from the operating system are used during substitution and will always override local variables defined within the
.envfile.
VAR=one
VAR_2=two
# Basic substitution
RESULT=$VAR # value: 'one'
# Using curly braces for non-alphanumeric names
RESULT=${VAR_2} # value: 'two'
# Escaping to prevent substitution
RESULT='$VAR' # value: '$VAR'
RESULT=\$VAR # value: '$VAR'
# OS environment variables override local definitions
# If $PATH is set in your shell, it will be used here
RESULT=$PATH