Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a multi-line environment variable on Windows?

I'm trying to set a private SSH key environment variable on Windows. The problem is the private SSH key is in a multiline textfile of the following form:

-----BEGIN RSA PRIVATE KEY-----
Line1
Line2
Line3
...
-----END RSA PRIVATE KEY-----

I've tried the following script to store the content of the text file in a variable so I can later use it to set an environment variable:

setlocal EnableDelayedExpansion

set "var="
set LF=^


rem *** Two empty lines are required for the linefeed
FOR /F "delims=" %%a in (private_key.txt) do (
  set "var=!var!!LF!%%a"
)
echo !var!

setx SSH_PRIVATE_KEY !var!

This however gives me the following error:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

Is there another way I can define such an environment variable?

like image 529
Metrician Avatar asked Oct 23 '25 11:10

Metrician


1 Answers

Unfortunately you left out a very important part of the error you've received, which is the second line in this example:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

if you type setx /? you will find the answer, however it is hidden specifically in this line:

    SETX /S system /U user /P password  BUILD /K
         "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\
         CurrentVersion\CurrentBuildNumber" /M

Note that the above line's path contains whitespace and the key here is that the path is double quoted. Your variable also contains whitespace and requires double quoting:

setx SSH_PRIVATE_KEY "!var!"

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!