bash Cygwin:使用包含 Windows 路径的路径变量(其中有一个空格)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4334467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cygwin: using a path variable containing a windows path (with a space in it)
提问by Guillaume
I want to add some dirs to my PATH. Unfortunately these dirs are located in windows path containing space (like the Documents and Settings)
我想在我的 PATH 中添加一些目录。不幸的是,这些目录位于包含空间的 Windows 路径中(如文档和设置)
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 ?
有什么建议吗?
采纳答案by RobS
This works:
这有效:
$ winhome="/cygdrive/c/Documents and Settings/"
$ cd "$winhome"
$ pwd
/cygdrive/c/Documents and Settings
回答by Amro
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...)
您可以使用cygpath
将 Windows 路径转换为 Cygwin 兼容的 POSIX 路径。它还可以输出一些特殊系统文件夹的位置(如Windows主目录、桌面、我的文档等...)
p="C:\Documents and Settings"
cd "$(cygpath -u "${p}")"
Here are some relevant links to Cygwin documentation:
以下是 Cygwin 文档的一些相关链接:
回答by Chris Cooper
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.
如果将路径放在引号中,则不需要对空格进行转义,但是在调用 时cd
,需要将变量本身放在引号中以获得正确的行为。
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:
这是因为变量在 shell 中被替换的方式。如果你cd winhome
没有" "
,一旦变量被替换,它最终看起来像这样:
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.
这被解析为四个单独的参数:cd
, /cygdrive/c/Documents
, and
, and Settings/43598811/
,这对 shell 没有意义,因为目录/cygdrive/c/Documents
不存在。
回答by mariofertc
For access to the path /cygdrive/c/Program Files (x86), use the \ character.
要访问路径 /cygdrive/c/Program Files (x86),请使用 \ 字符。
$ cd /cygdrive/c/Program\ Files\ \(x86\)
Good Day!
再会!
回答by NandR
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.
现在可能很晚了,但在过去的几年里,我对这个问题使用了不同的解决方案。在 cygwin.bat 中,我添加了一些语句以使用subst
命令创建虚拟驱动器。使用此命令,您可以将驱动器号替换为非常长的路径(以及包含空格或特殊 Win 字符的路径)。稍后您可以在您的 shell 和脚本中直接将它们引用为/cygdrive/x/
、/cygdrive/y/
等。这也有助于从资源管理器等 windows shell 获取这些目录的快捷方式。
On a related note, if you want, you can get rid of /cygdrive
prefix by using
mount
command from Cygwin. see mount --help
.
在相关说明中,如果需要,您可以/cygdrive
使用mount
Cygwin 中的命令去掉前缀
。见mount --help
。
回答by Patrick Bergner
Use cygpath --mydocs
.
使用cygpath --mydocs
.
Other special folders according to cygpath --help
:
其他特殊文件夹根据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
回答by ErichBSchulz
I am a complete cwywin noob - but surely creating a symlink back to your windows home has to be an easy first step?
我是一个完整的 cwywin 菜鸟 - 但肯定创建一个符号链接回到你的 windows home 必须是一个简单的第一步?
ie:
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?
你可以为你的桌面等做同样的事情......并且不再担心 win/cyg 转换?
editnow 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
现在编辑我已经使用了cygpath
- 我相信这是要使用的工具 - 无论有没有符号链接,它都是一个用于处理 *nix/wind path disjoint 的简洁小包
回答by Ringding
The approach with the variable should work if you defined it correctly. Yours contains backslashes for no good reason.
如果您正确定义了变量,则该方法应该可以工作。你的包含反斜杠没有充分的理由。
回答by Abhay Dandekar
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
Linux 不喜欢空格,windows 也不关心它。古老的War!伙计们,我建议不要卷入这场War或在这场War中下任何赌注。:P
Solution is simple, just create a symlink in cygwin and start using it.
解决方案很简单,只需在 cygwin 中创建一个符号链接并开始使用它。
For e.g: My builds were failing because I exported JAVA_HOME as below
例如:我的构建失败了,因为我将 JAVA_HOME 导出如下
$ export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_144"
$ export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_144"
This didnt work from cygwin, because there was a space in path.
这在 cygwin 中不起作用,因为路径中有一个空格。
To overcome this, I created a symlink to Program Files under my home dir.
为了克服这个问题,我在我的主目录下创建了一个指向 Program Files 的符号链接。
$ cd ~
$ cd ~
$ ln -s /cygdrive/c/Program\ Files ProgramFiles
$ ln -s /cygdrive/c/Program\ 文件 ProgramFiles
$ export JAVA_HOME="/home/adandekar/ProgramFiles/Java/jdk1.8.0_144"
$ export JAVA_HOME="/home/adandekar/ProgramFiles/Java/jdk1.8.0_144"
This worked seamlessly. Hope this helps!
这无缝地工作。希望这可以帮助!