POSIX-compliant .env syntax is subset of POSIX shell scripts
UTF-8 is already widespread and there is no need to support other encodings.
POSIX shells treats CR as a character.
# when the newline code is CR LF
FOO=123
echo "${#FOO}" # => 4
# dotenv posix
FOO=123
The shell cannot handle the null (\0
) character.
POSIX shell specification
Valid
FOO=
Invalid
FOO
POSIX shell specification.
Valid
FOO=123
POSIX shell specification.
Invalid
FOO =123
BAR= 123
POSIX shell specification.
Invalid
FOO.BAR.BAZ=123
1ABC=123
Many dotenv implementations support it.
Valid
export FOO=123
export BAR # Same as `export BAR="${BAR:-}"`
POSIX shell specification.
Valid
# comment
# comment
POSIX shell specification.
Valid
FOO=123 # this is comment
BAR='123' # this is comment
BAZ="123" # this is comment
Invalid
FOO=123#this-is-not-comment
BAR='123'#this-is-not-comment
BAZ="123"#this-is-not-comment
POSIX shell specification.
Invalid
FOO=123 456
The following characters have a special meaning in shell scripts and cannot be used without quotes.
[ ] { } ( ) < > " ' ` ! $ & ~ | ; \ * ?
In the POSIX shell specification, it can be used by escaping it with a backslash, but it is better to use quotes.
Invalid
URL=http://example.com?name1=value1&name2=value2
Note: #
(without preceding spaces or tabs) can be used as a character.
Valid
URL1='http://example.com?name1=value1&name2=value2'
URL2=http://example.com/index.html#this-is-hash #this-is-comment
The behavior is different for different shells, so it is not portable.
VAR="foo bar"
export VALUE=$VAR
echo "$VALUE"
# => dash, posh, yash: `foo`
# => bash, ksh, mksh, zsh: `foo bar`
POSIX shell specification. Use double quotes.
Invalid
VAR1='foo'bar'
VAR2='foo\'bar'
POSIX shell specification.
Invalid
VAR='${FOO}' # this is just a string
Valid
VAR='line1
line2'
This is possible in POSIX shell specification, but prohibited for simplicity of the specification.
Invalid
VAR='foo''bar'
POSIX shell specification.
Invalid
VAR=" " ` \ $ "
Valid
VAR=" \" \` \\ \$ "
POSIX shell specification.
VAR=" \a \r \n "
printf '%s' "$VAR" # => \a \r \n
NOTE: Do not investigate with echo
. In some shells, echo
may interpret escape characters.
POSIX shell specification.
LONGLINE="https://github.com/ko1nksm\
/shdotenv/blob/main/README.md"
echo "$LONGLINE" # => https://github.com/ko1nksm/shdotenv/blob/main/README.md
POSIX shell specification.
Valid
BAR="bar"
VALUE="foo ${BAR} baz"
If there are no braces, the behavior may vary depending on the shell.
BAR="bar"
VALUE="foo $BAR[0] baz"
# => bash: foo bar[0] baz
# => zsh: foo baz
POSIX shell specification
unset BAR
VALUE="foo ${BAR} baz"
echo "$VALUE" # => foo baz
It is not supported due to its complexity and the security risk of random command execution.