Cygwin:使用包含windows路径的路径变量(其中包含空格)

时间:2010-12-02 11:29:25

标签: bash configuration path cygwin

我想在PATH中添加一些目录。 不幸的是,这些目录位于包含空间的窗口路径中(如文档和设置)

我没有成功尝试:

创建变量:

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

创建别名:

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

使用软链接 它正在工作......但我不想使用这个

有什么建议吗?

9 个答案:

答案 0 :(得分:25)

这有效:

$ winhome="/cygdrive/c/Documents and Settings/"
$ cd "$winhome"
$ pwd
/cygdrive/c/Documents and Settings

答案 1 :(得分:16)

您可以使用cygpath将Windows路径转换为与Cygwin兼容的POSIX路径。它还可以输出一些特殊系统文件夹的位置(例如Windows主目录,桌面,我的文档等)。

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

以下是Cygwin文档的一些相关链接:

答案 2 :(得分:9)

如果将路径放在引号中,则不需要转义空格,但是当您调用cd时,需要将变量本身放在引号中以获得正确的行为。

因此,您的变量应该像这样声明,但是使用变量周围的引号调用:

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

这是因为变量在shell中被替换的方式。如果你在没有cd winhome的情况下" ",那么一旦变量被替换,它就会看起来像这样:

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

这会被解析为四个单独的参数:cd/cygdrive/c/DocumentsandSettings/43598811/,这对shell没有意义,因为目录{{1} }不存在。

答案 3 :(得分:6)

要访问路径/ cygdrive / c / Program Files(x86),请使用\字符。

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

美好的一天!

答案 4 :(得分:5)

现在可能已经很晚了,但我对这个问题采用了不同的解决方案 过去几年。在cygwin.bat中,我用几个语句作为前缀来创建 使用subst命令的虚拟驱动器。使用此命令,您可以替换a 很长的路径的驱动器号 (以及包含空格或特殊Win字符的那些)。稍后你可以参考 它们来自您的shell和脚本,直接作为/cygdrive/x//cygdrive/y/和 等等。这也有助于从Windows获取这些目录的快捷方式 shell就像探险家等。

在相关的说明中,如果您愿意,可以使用删除/cygdrive前缀 来自Cygwin的mount命令。见mount --help

答案 5 :(得分:3)

使用cygpath --mydocs

根据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

答案 6 :(得分:2)

我是一个完整的cwywin菜鸟 - 但是确实创建一个符号链接回到你家的窗口必须是一个简单的第一步?

即:

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

然后你可以:

 ls wh

你可以为你的桌面等做同样的事情......并且不再担心胜利/小天才转换?

编辑现在我使用了cygpath - 我相信这是使用的工具 - 有或没有符号链接它是一个整洁的小包来处理* nix / wind路径不相交的

答案 7 :(得分:1)

如果您正确定义了变量,那么变量的方法应该有效。你没有充分的理由包含反斜杠。

答案 8 :(得分:0)

Linux不喜欢空格,Windows不关心空格。古老的战争! 伙计们,我建议不要陷入这场战争或在这场战争中下任何赌注。 :P

解决方案很简单,只需在cygwin中创建一个符号链接并开始使用它。

例如:我的构建失败,因为我按如下所示导出了JAVA_HOME

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

这对cygwin无效,因为路径中有空格。

为克服此问题,我在主目录下创建了指向Program Files的符号链接。

$ cd〜

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

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

这无缝进行。 希望这会有所帮助!

相关问题