Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign array value to ENV var on .env file

I need to set an array of strings on my .env file and cant find information about the right syntax. Test for this takes quite a while so I wanted to save some time. Some of this options should work:

MY_ARRAY=[first_string, second_string]
MY_ARRAY=[first_string second_string]
MY_ARRAY=['first_string', 'second_string']

Can someone tell me which?

like image 715
ntonnelier Avatar asked Jan 27 '26 11:01

ntonnelier


1 Answers

As far as I know dotenv does not allow setting anything except strings (and multiline strings). The parser syntax is:

LINE = /
  \A
  (?:export\s+)?    # optional export
  ([\w\.]+)         # key
  (?:\s*=\s*|:\s+?) # separator
  (                 # optional value begin
    '(?:\'|[^'])*'  #   single quoted value
    |               #   or
    "(?:\"|[^"])*"  #   double quoted value
    |               #   or
    [^#\n]+         #   unquoted value
  )?                # value end
  (?:\s*\#.*)?      # optional comment
  \z
/x

The reason behind this is shell and OS support for setting other types of env variables is spotty.

You could use a separator such as commas or pipes (|) and split the string with ENV['FOO'].split('|'). But maybe what you are trying to do should be solved with an initializer which combines ENV vars.

like image 126
max Avatar answered Jan 29 '26 05:01

max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!