Dotenv multiline variables

35,263

Solution 1

Did you try reading the documentation?

Multi-line values

If you need multiline variables, for example private keys, you can double quote strings and use the \n character for newlines:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9…\n-----END DSA PRIVATE KEY-----\n"

Solution 2

From the documentation Brian posted above:

Alternatively, multi-line values with line breaks are now supported for quoted values.

So the solution you sketched in your question is legit now!

Share:
35,263

Related videos on Youtube

Mirror318
Author by

Mirror318

Coding between the lines. I'm living and loving Ruby on Rails, always learning and especially interested in software design/architecture. Outside of work I enjoy singing, cooking, electronics, and sunshine. I live, and yet not I but Christ that lives in me.

Updated on July 09, 2022

Comments

  • Mirror318
    Mirror318 almost 2 years

    I'm using dotenv.

    A Ruby gem to load environment variables from .env.

    Is it possible to have multiline variables in my .env file?

    e.g.

    SOMETHING_CERTIFICATE="-----BEGIN CERTIFICATE-----
    JSDFALDAFSSKLABVCXZLV2314IH4IHDFG9AYDF9DSSDF82QWEIWFHDSSD8SADF0=
    -----END CERTIFICATE-----"
    

    ^ having the above just throws an error on that middle line, as if it's not part of the string and I'm trying to create an improperly formatted variable.

    • Gokul
      Gokul over 6 years
      Did you tried "\n"?
    • Mirror318
      Mirror318 over 6 years
      I did try "\n", it just came up in the error message automatically parsed to "\\n"—but that's because I tried to keep the whitespace newlines as well as the "\n"s. Putting it all on a single line with the "\n" worked, although it's not really what i wanted, it gets ugly for long strings.
  • Gundam Meister
    Gundam Meister almost 6 years
    To add to Brian answer. If you need to consume the PRIVATE_KEY in a config file for example, you can do something like this: signing_key "<<-EOL\n" + "#{ENV['PRIVATE_KEY']}".gsub("\\n","\n") + "EOL\n"
  • mxcl
    mxcl over 2 years
    Answers containing “Did you try reading the documentation” do not deserve upvotes.
  • davetapley
    davetapley over 2 years
    sed version for the lazy: cat id_rsa | sed -z -e 's/\n/\\n/g'
  • MrMesees
    MrMesees over 2 years
    Weirdly, in a docker setup, me and my colleague (same OS) are getting inconsistencies with that. I Think it might be safest to add the newlines.