Set a Windows environment variable from within Git Bash

Is it possible to set an actual Windows environment variable from within the Git Bash for example? I know that you can set environment variables via the Git Bash in general with e.g.

FOOBAR=foo; export FOOBAR;

But these environment variables will only be available within the Git Bash environment, not within Windows in general. So if you open the regular Windows Command Prompt afterwards and execute

echo %FOOBAR%

it will not output foo.

The reason for this question: I am starting the SSH Agent with a bash script in ~/.bashrc within Windows. This allows me to then connect to any SSH servers I have access to with my SSH key within the Git Bash. However, any applications that are not run from within Git Bash cannot access the already running SSH Agent, because the SSH_AUTH_SOCK environment variable will not be available outside the Git Bash.

I know the solution in this case would probably be to run the SSH Agent in Windows "natively" (and then I would hope/assume that the SSH Agent is also available on the Git Bash), but I am still curious about whether you can set environment variables from within the Git Bash - that will also be available in the rest of the system.

// edit: I also tried the following on the Git Bash:

cmd //c set FOOBAR=foo

(see here why you need to use a double slash), but still no luck when I open up CMD afterwards:

echo %FOOBAR%
%FOOBAR%

1 Answer

One solution would be to use

setx FOOBAR foo

However, this will set a permanent user environment variable. In my case this is fine. Everytime the Git Bash is started, which also starts the SSH Agent, the script now also sets the SSH_AUTH_SOCK environment variable as a user environment variable via

setx SSH_AUTH_SOCK ${SSH_AUTH_SOCK}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like