Skip to content

Can't escape single quotes in single quote string #442

Closed

Description

@jordie23

If I have a value (using the bash way to "escape" a single quote in a single quote string):

BLARG='hey'\''string'

I get the following error:

Fatal error: Uncaught Dotenv\Exception\InvalidFileException: Failed to parse dotenv file due to unexpected whitespace. Failed at ['hey'\''string']. in /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php on line 119

Even if I try this, which doesn't work in bash, but would be more in line with double quotes I still get an error:

BLARG='hey\'string'

One or the other should be supported. Preferably the former as it's more inline with bash interpretation of env vars, so the same env file can be used by bash scripts and php.

I'm using single quotes to avoid interpolation by double quotes.

I'm using 4.1.6

Activity

  1. GrahamCampbell commented on May 25, 2020

    @GrahamCampbell
    Collaborator

    Yep, this is correct. Single quote strings do not accept escape characters, in line with what bash does. See https://linuxhint.com/bash_escape_quotes/.

  2. GrahamCampbell commented on May 25, 2020

    @GrahamCampbell
    Collaborator

    I'm using single quotes to avoid interpolation by double quotes.

    You can avoid interpolation by escaping the dollars signs in double quote mode. See https://github.com/vlucas/phpdotenv/blob/v4.1.6/tests/fixtures/env/nested.env and https://github.com/vlucas/phpdotenv/blob/v4.1.6/tests/Dotenv/DotenvTest.php#L180-L197 for examples of how this works. In particular, look at NVAR4 and NVAR15. :)

  3. GrahamCampbell commented on May 25, 2020

    @GrahamCampbell
    Collaborator

    NB This has been available in version 4.0.0, implemented in #380.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions