Cygwin:使用包含Windows路径的路径变量(其中有空格)
我想将一些目录添加到我的路径中。 不幸的是,这些目录位于包含空格的 Windows 路径中(如文档和设置),
我尝试失败:
创建变量:
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
使用软链接: 它正在工作......但我不想使用这个
有什么建议吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这有效:
This works:
您可以使用
cygpath
转换 Windows 路径到与 Cygwin 兼容的 POSIX 路径中。它还可以输出一些特殊系统文件夹的位置(例如 Windows 主目录、桌面、我的文档等...)以下是 Cygwin 文档的一些相关链接:
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...)Here are some relevant links to Cygwin documentation:
如果将路径放在引号中,则不需要转义空格,但是当您调用
cd
时,需要将变量本身放在引号中以获得正确的行为。因此,您的变量应该像这样简单地声明,但在变量周围使用引号来调用:
这是因为变量在 shell 中被替换的方式。如果您在不使用
" "
的情况下执行cd winhome
,那么一旦变量被替换,它最终会看起来像这样:这将被解析为四个单独的参数:
cd、
不存在。/cygdrive/c/Documents
、and
和Settings/43598811/
,这对 shell 没有任何意义,因为目录 <代码>/cygdrive/c/DocumentsIf 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:
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:This gets parsed as four separate arguments:
cd
,/cygdrive/c/Documents
,and
, andSettings/43598811/
, which makes no sense to the shell because the directory/cygdrive/c/Documents
does not exist.要访问路径 /cygdrive/c/Program Files (x86),请使用 \ 字符。
再会!
For access to the path /cygdrive/c/Program Files (x86), use the \ character.
Good Day!
现在可能已经很晚了,但我对这个问题使用了不同的解决方案
过去几年。在 cygwin.bat 中,我添加了一些语句来创建
使用
subst
命令的虚拟驱动器。使用此命令,您可以替换很长路径的驱动器号
(以及包含空格或特殊 Win 字符的内容)。稍后你可以参考
它们来自您的 shell 和脚本,直接作为
/cygdrive/x/
、/cygdrive/y/
和很快。这也有助于从 Windows 获取这些目录的快捷方式
shell 像资源管理器等。
在相关说明中,如果您愿意,您可以使用以下命令摆脱
/cygdrive
前缀来自 Cygwin 的
mount
命令。请参阅mount --help
。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 adrive 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/
, andso 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 usingmount
command from Cygwin. seemount --help
.使用 cygpath --mydocs 。
其他特殊文件夹根据
cygpath --help
:Use
cygpath --mydocs
.Other special folders according to
cygpath --help
:我是一个十足的cwywin菜鸟 - 但创建一个回到你的Windows主页的符号链接肯定是一个简单的第一步?
即:
那么你可以:
你可以为你的桌面等做同样的事情......并且不再担心 win/cyg 转换?
编辑现在我已经使用了
cygpath
- 我相信这是一个可以使用的工具 - 无论有没有符号链接,它都是一个用于处理 *nix/wind 路径的简洁小包不相交的I am a complete cwywin noob - but surely creating a symlink back to your windows home has to be an easy first step?
ie:
then you can just:
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如果您定义正确,则使用变量的方法应该有效。您的内容无缘无故地包含反斜杠。
The approach with the variable should work if you defined it correctly. Yours contains backslashes for no good reason.
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"
这一切顺利进行。
希望这有帮助!
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!