Environment variable with spaces

Hi! Not sure if it’s a bug, but here’s a report of a strange issue I just experienced

I have a private key in .env that contains spaces, so I wrap it into quotes:

PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nEEIAJDIO...."

When I try to use it, the \n are not translated to new lines, so the key is invalid.

console.log(process.env.PRIVATE_KEY) 
//-----BEGIN PRIVATE KEY-----\nEEIAJDIO....

I have to do this replacement, which oddly enough seems to work, suggesting that new lines are escaped:

process.env.PRIVATE_KEY=process.env.PRIVATE_KEY.replace(/\\n/g, '\n') 
console.log(process.env.PRIVATE_KEY) 
//-----BEGIN PRIVATE KEY-----
//EEIAJDIO...."

That said, is there a way to use private environment variables with spaces?

.env is a Bash script, so you can use Bash-specific string encoding:

PRIVATE_KEY=$'-----BEGIN PRIVATE KEY-----\nEEIAJDIO....'
1 Like

Thank you, that worked! :ok_hand:

1 Like