aitorpazos
aitorpazos

Reputation: 174

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.

Upvotes: 9

Views: 6629

Answers (2)

Sungam
Sungam

Reputation: 1734

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-`")"

Upvotes: 1

Edward Anderson
Edward Anderson

Reputation: 13926

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-`")"

Upvotes: 3

Related Questions