22

For instance, Symfony's configuring-environment-variables-in-env-files documents provides examples:

DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name"
DB_USER=root

Why are quotes (aka parentheses) used for the first example and not the second?

If the applicable answer is dependent upon the application parsing the .env file, please base it on Symfony.

user1032531
  • 24,767
  • 68
  • 217
  • 387
  • 2
    The documentation says .env files are just shell scripts, meaning they use shell script syntax. If you want finer details on that, read tutorials or documentation for shell scripts on unix (or windows if you are using windows). – siride Mar 19 '22 at 14:05
  • @siride Before posting this question, I did look over multiple documents and I was under the impression that it might be based on the application. If it is just a linux/unix thing, I will focus there. Thank you for your comment. – user1032531 Mar 19 '22 at 14:09

1 Answers1

39

TL;DR solution: Use the quotation marks when the string contains spaces or certain special character and certain syntaxes. These include:

  • space and other whitespace,

  • backslash (escapes a space and newline – \ gives space even in unquoted string),

  • quotation marks (but you can combine multiple quotation mark\ 'styles like'"this"),

  • pound sign (#) that marks start of comment (if it is not in quotes string or $(…)),

  • dollar sign (that is used to expand a variable – see below),

  • parentheses (( and )) – depending on context,

  • shell redirection chars (>, <, 2>, | etc.),

  • asterisk (*) and question mark (?), since it is used in globs,

  • square brackets (because they list characters),

  • comma-separated text in {…} (because it provides multiple variants of text – {foo,bar}baz expands to foobaz barbaz),

  • maybe others,

  • and of course, the newline.


.env files are, according to the page linked by you, regular bash scripts. That means that:

  • One string can't contain multiple words (space-separated parts), unless it is enclosed in quotes.¹

    FOO_VAR='multiple words'    # This works.
    ANOTHER_VAR="foo bar"       # This works, too.
    
    BAR_VAR=this does not work  # Executes “does” with args
                                # “not work” and variable
                                # BAR_VAR=“this”.
    
  • Variable expansion is performed if the text is enclosed in double quotes or it is not enclosed in any quotes.

    my_var=42
    VARIABLES="foo ${my_var}"  # Gives “foo 42”.
    
  • Shell commands can be executed to produce the strings.²

    CURRENT_DATE="$(date)"  # Executes “date” and uses its
                            # stdout as the value.
    DO_NOT_DO_THIS=$(date)  # First expands the command, and we
                            # then get multiple unquoted words.
    AVOID_THIS="`date`"  # Non-standard syntax, accepted by bash.
    

¹ In the other case, it will run the second and all later “words” as a shell command with the given variable. Refer to bash(1) manpage.

² According to the docs, it is not supported on Microsoft Windows. It does not say anything about variable expansion syntax on Windows.

jiwopene
  • 3,077
  • 17
  • 30
  • Thanks. What are considered special characters? – user1032531 Mar 19 '22 at 14:07
  • @user1032531, see the edit. The rule is simple: if it is not only one word, single-quote it. If you want variable expansion, **always** double-quote it. – jiwopene Mar 19 '22 at 14:18
  • 2
    The `docker run` subcommand has problems with environment variables with quotes and double quotes, as it does not accept *env files* formatted as valid BASH ("Shell") scripts. If this is your case, this answer may interest you https://stackoverflow.com/a/75237297/3223785 . – Eduardo Lucio Jan 25 '23 at 16:56