Dotenv multiline variables
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!
Related videos on Youtube
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, 2022Comments
-
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 over 6 yearsDid you tried "\n"?
-
Mirror318 over 6 yearsI 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 almost 6 yearsTo 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 over 2 yearsAnswers containing “Did you try reading the documentation” do not deserve upvotes.
-
davetapley over 2 years
sed
version for the lazy:cat id_rsa | sed -z -e 's/\n/\\n/g'
-
MrMesees over 2 yearsWeirdly, 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.