33

I want to add some dirs to my PATH. Unfortunately these dirs are located in windows path containing space (like the Documents and Settings)

I've unsuccessfully tried to:

Create a variable:

43598811@E250BZD20015026 ~
$ winhome="/cygdrive/c/Documents\ and\ Settings/43598811/"

43598811@E250BZD20015026 ~
$ cd $winhome
bash: cd: /cygdrive/c/Documents\: No such file or directory    

43598811@E250BZD20015026 ~
$ cd "$winhome"
bash: cd: /cygdrive/c/Documents\ and\ Settings/43598811/: No such file or directory

Create an alias:

43598811@E250BZD20015026 ~
$ alias winhome="/cygdrive/c/Documents\ and\ Settings/43598811/"

43598811@E250BZD20015026 ~
$ winhome
bash: /cygdrive/c/Documents and Settings/43598811/: is a directory

43598811@E250BZD20015026 ~
$ cd winhome
bash: cd: winhome: No such file or directory

Use a soft link: it is working... but I don't want to use this

Any suggestion ?

Guillaume
  • 5,488
  • 11
  • 47
  • 83

9 Answers9

28

This works:

$ winhome="/cygdrive/c/Documents and Settings/"
$ cd "$winhome"
$ pwd
/cygdrive/c/Documents and Settings
RobS
  • 3,807
  • 27
  • 34
22

You can use cygpath to convert Windows path into Cygwin-compatible POSIX paths. It can also output the locations of some special system folders (such as Windows home directory, desktop, my documents, etc...)

p="C:\Documents and Settings"
cd "$(cygpath -u "${p}")"

Here are some relevant links to Cygwin documentation:

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thanks for the tip. However I'm still stuck with ugly command: cd "$(cygpath -u "${p}")". I wand to store that in a var and use it in other script or cmd without to have to remember the syntax... – Guillaume Dec 02 '10 at 16:57
  • 2
    `p2="$(cygpath -u "${p}")"` then `cd "$p2"` – Amro Dec 02 '10 at 18:01
  • 2
    It seems (not checking any docs) Cygwin is able to auto convert Windows paths and filespecs when they are arguments, but not if you need to run something. stat "$UTIL", file "$UTIL", anything goes, but not just "$UTIL". I know this wasn't requested by the OP, but this answer is the only way to *run* something and covers the case I described. – Charles Roberto Canato May 25 '16 at 23:14
  • @CharlesRobertoCanato Generally Cygwin programs understand both POSIX and Win32-style paths (although it is deprecated to use win-paths). When calling native Windows programs from a Cygwin shell (like calling notepad.exe), they obviously don't understand POSIX paths, and must be explicitly converted.. I edited the answer to point to some relevant docs. – Amro May 26 '16 at 14:38
  • Also note that MSYS/MinGW has its own heuristic for automatic path conversion, unrelated to Cygwin: http://www.mingw.org/wiki/Posix_path_conversion – Amro May 26 '16 at 14:39
  • @Amro, I added info but explained it terribly. More specifically, I use Docker Toolbox and in its start script, it calls 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' through a variable (it's not arguments that cause problems here, but the executable calling itself). It does this cause it uses MSYS by default. This call to an application does not work under Cygwin as it is. You must convert the backslashes to slashes ('C:/Program...../VBoxManage.exe') or you might use this answer. ;-) – Charles Roberto Canato Jun 04 '16 at 07:13
  • @CharlesRobertoCanato Ah I see. I have no experience with Docker, but in Cygwin the convention is to call Windows programs as: `/cygdrive/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe file.txt`. There's also `run` and `cygstart` commands for starting GUI vs. console programs. Like I mentioned, MSYS is a bit different (for one it doesn't use the `/cygdrive/c/` prefix, just `/c/`).. – Amro Jun 04 '16 at 16:14
9

If you put the path in quotation marks, you don't need to escape the spaces, but when you call cd, you need to put the variable itself in quotes to get the proper behaviour.

So your variable should simply be declared like this, but called using quotes around the variable:

~>  winhome="/cygdrive/c/Documents and Settings/43598811/"
~>  cd "$winhome"

This is because of the way variables get substituted in the shell. If you do cd winhome without the " ", it ends up looking like this once the variable get substituted:

cd /cygdrive/c/Documents and Settings/43598811/

This gets parsed as four separate arguments: cd, /cygdrive/c/Documents, and, and Settings/43598811/, which makes no sense to the shell because the directory /cygdrive/c/Documents does not exist.

Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
6

For access to the path /cygdrive/c/Program Files (x86), use the \ character.

$ cd /cygdrive/c/Program\ Files\ \(x86\)

Good Day!

mariofertc
  • 383
  • 2
  • 7
5

Probably very late now, but I use a different solution to this problem for the last several years. In the cygwin.bat I prefix a few statements to create virtual drives using subst command. Using this command, you can substitute a drive letter for really long paths (and those containing spaces or special Win characters). Later you can refer to them from your shell and scripts directly as /cygdrive/x/, /cygdrive/y/, and so on. This also helps in getting shortcuts to these directories from windows shell like explorer etc.

On a related note, if you want, you can get rid of /cygdrive prefix by using mount command from Cygwin. see mount --help.

Zombo
  • 1
  • 62
  • 391
  • 407
NandR
  • 51
  • 1
  • 1
3

Use cygpath --mydocs.

Other special folders according to cygpath --help:

-A, --allusers        use `All Users' instead of current user for -D, -O, -P
-D, --desktop         output `Desktop' directory and exit
-H, --homeroot        output `Profiles' directory (home root) and exit
-O, --mydocs          output `My Documents' directory and exit
-P, --smprograms      output Start Menu `Programs' directory and exit
-S, --sysdir          output system directory and exit
-W, --windir          output `Windows' directory and exit
Patrick Bergner
  • 623
  • 8
  • 23
2

I am a complete cwywin noob - but surely creating a symlink back to your windows home has to be an easy first step?

ie:

 wh="/cygdrive/c/Documents and Settings/Erich/My Documents"
 ln -s "$wh" wh

then you can just:

 ls wh

you could do the same thing for your desktop etc... and worry no more about the win/cyg conversion?

edit now I've used cygpath - I beleive this is the tool to use - with or without symlinks it is a neat little package for dealing with the *nix/wind path disjoint

ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61
  • 4
    You can even use native NTFS symlink as explained in [answer about `CYGWIN="winsymlinks:native"`](http://stackoverflow.com/a/18660841/938111). By default, Cygwin simulates symlink storing the destination path a regular within a regular file. However non-Cygwin applications are not compliant with the Cygwin workaround. @Guillaume @ErichBSchulz Using native NTFS symlink enables sharing symlinks between native Windows apps and Cygwin ones. Cheers – oHo Sep 26 '13 at 09:26
1

The approach with the variable should work if you defined it correctly. Yours contains backslashes for no good reason.

Ringding
  • 2,856
  • 17
  • 10
  • yeah - they look like an attempt to escape (ie quote) the spaces - but not needed since in the original post the entire path is quoted - I think if the initial paths were taken out of the double quotes the the backslashed spaces would actually make it work – ErichBSchulz Jun 14 '12 at 11:21
  • Either quote the entire dir or escape the backslashes - not both! – Adam Nov 15 '17 at 15:58
0

Linux does not like spaces, and windows doesn't care about it. Age old war! Guys, I would recommend not falling into this war or put any wager across this one. :P

Solution is simple, just create a symlink in cygwin and start using it.

For e.g: My builds were failing because I exported JAVA_HOME as below

$ export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_144"

This didnt work from cygwin, because there was a space in path.

To overcome this, I created a symlink to Program Files under my home dir.

$ cd ~

$ ln -s /cygdrive/c/Program\ Files ProgramFiles

$ export JAVA_HOME="/home/adandekar/ProgramFiles/Java/jdk1.8.0_144"

This worked seamlessly. Hope this helps!

Abhay Dandekar
  • 1,230
  • 10
  • 30