Setting a path with whitespace in Cygwin

I set 2 environment variables to test which one works for me, as following

.bash_profile

NODE_BIN1="/cygdrive/c/Program Files/nodejs"
NODE_BIN2=/cygdrive/c/Program\ Files/nodejs
export NODE_BIN1 NODE_BIN2

then test them in Cygwin terminal

$ cd $NODE_BIN1
kevin@kevin-HP /cygdrive/c/Program  (wrong!)

$ cd $NODE_BIN2
kevin@kevin-HP /cygdrive/c/Program  (wrong!)

$ cd C:/Program Files/nodejs
kevin@kevin-HP /cygdrive/c/Program  (wrong!)

$ cd "C:/Program Files/nodejs"
kevin@kevin-HP /cygdrive/c/Program Files/nodejs 

The last result is what I want but actually it's same string as $NODE_BIN1.

Any idea to fix this ? Thanks a lot !

Try using cygpath?

export NODE_BIN1=`cygpath -w -s "/cygdrive/c/Program Files/nodejs"`

This also provides the same output

export NODE_BIN1=`cygpath -d "/cygdrive/c/Program Files/nodejs"`

Both approaches will set the environment variable correctly. The problem you're experiencing is when you try to use it; bash will split variables on spaces by default, and you end up calling cd with two arguments: /cygdrive/c/Program and Files/nodejs.

The solution, of course, is to switch to zsh. ;)

Okay, okay. If your intention is to be able to switch to this directory with ease, consider writing an alias instead.

alias cdnode='cd "/cygdrive/c/Program\ Files/nodejs"'

If you only want to set this for node's benefit, then don't worry; you're already good to go. You can be absolutely sure using echo instead.

$ echo "[$NODE_BIN1]"
[/cygdrive/c/Program\ Files/nodejs]