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.
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.
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.
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-`")"
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-`")"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With