9

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.

aitorpazos
  • 164
  • 1
  • 6
  • Finally I did it using: **export ${var_name}="`reg query HKCU\\Environment /v ${var_name} | grep ${var_name} | cut -c23- | cygpath -pu `";** It's not clean but it works in most cases. If VARVALUE is not a path cygpath will leave it unchanged unless it looks like a path but it shouldn't be changed, in that case it will mess everything but that's not a common situation – aitorpazos Jan 20 '12 at 14:47

2 Answers2

3

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-`")"
Edward Anderson
  • 13,591
  • 4
  • 52
  • 48
  • 1
    The `cut` 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-`")"``` – Sungam Oct 19 '16 at 19:57
1

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-`")"
Sungam
  • 1,684
  • 1
  • 21
  • 24