phpdotenv icon indicating copy to clipboard operation
phpdotenv copied to clipboard

Unable to escape backtick

Open oliviakim321 opened this issue 5 years ago • 2 comments

Backticks in variables enclosed in double quotes cannot be escaped with a backslash. I would like to be able to escape backticks so that my .env file can be sourced as well as parsed with Dotenv.

in my .env file:

NAME="world"
GREETING="\`hello ${NAME}\`"
$ source .env
$ echo $GREETING
`hello world`

But, when the \ is present before the backticks, I get the following error from Dotenv:

>>> Dotenv::createImmutable(dirname(__DIR__), '.env')->load();
Dotenv\Exception\InvalidFileException: Failed to parse dotenv file due to an unexpected escape sequence. Failed at ["\`hello ${NAME}\`"].

If I remove the \ before the backtick, I am unable to run source .env, since hello is interpreted as a command rather than literally:

$ source .env
.env:2: command not found: hello

Single quotes could be used to escape the backticks, but then the ${NAME} would not be expanded.

oliviakim321 avatar Jan 12 '21 16:01 oliviakim321

You can just write the backtick as is, in single quotes.

NAME="world"
GREETING="`hello ${NAME}`"

GrahamCampbell avatar Jan 12 '21 16:01 GrahamCampbell

phpdotenv does not aim for compatibility with source. We have our own feature set and syntax. It is a similar situation for the most popular ruby and python env file parsers too.

GrahamCampbell avatar Jan 12 '21 16:01 GrahamCampbell