Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh windows user's environment variables in cygwin

I would like to refresh cygwins environment after doing a setx VARNAME VARVALUE (specially paths).

'export VARNAME=VARVALLUE' is not an option because I would need to transform the exported value if it's a path(to UNIX like format), but VARNAME can be a path or not.

I would like to run setx and then refresh the environment so cygwin performs the corresponding transformations if VARNAME is PATH.

like image 532
aitorpazos Avatar asked Jan 20 '12 09:01

aitorpazos


People also ask

How do you refresh environment variables?

To refresh the environment variables in PowerShell, retrieves the environment variables and assign them $Env:Path to reload the environment variable path in the PowerShell. After reloading the path in PowerShell, you don't need to restart the PowerShell ISE or terminal.

Does .ENV file work in Windows?

Windows do not allow you to create a . env file directly from the windows explorer since it will not allow file names starting with a dot. However, you will be able to create it from VSCode easily.


2 Answers

To build on Apiman's answer, it's more likely in general you'll find the PATH in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment instead, which contains the system PATH instead of the User's PATH. I've also made a few corrections below.

Run this in the cygwin environment to load the Windows system PATH (or other environment variables by changing var_name)

export var_name="PATH"
export $var_name="$(cygpath -pu "`reg query 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' /v $var_name | grep $var_name | cut -c23-`")"

Of course with the, code above, the windows PATH will replace the local PATH, making you lose access to cygwin /bin and others. Instead, you probably want to append the Windows PATH to the cygwin PATH:

export PATH="$PATH:$(cygpath -pu "`reg query 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' /v PATH| grep PATH | cut -c23-`")"
like image 145
Edward Anderson Avatar answered Sep 27 '22 18:09

Edward Anderson


Added comment above but the formatting is not good. Repost here.

The cut in @nilbus' answer doesn't work for me. In my Win7, there are 30 characters before the real Path. I used this instead

export PATH="$PATH:$(cygpath -pu "`reg query \
 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' \
 /v PATH|grep PATH|sed 's| \+| |g'|cut -d" " -f4-`")"
like image 42
Sungam Avatar answered Sep 27 '22 20:09

Sungam