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

发布于 2024-10-05 19:20:53 字数 865 浏览 5 评论 0原文

我想将一些目录添加到我的路径中。 不幸的是,这些目录位于包含空格的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

吐个泡泡 2024-10-12 19:20:53

这有效:

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

This works:

$ winhome="/cygdrive/c/Documents and Settings/"
$ cd "$winhome"
$ pwd
/cygdrive/c/Documents and Settings
贪了杯 2024-10-12 19:20:53

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

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

以下是 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...)

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

Here are some relevant links to Cygwin documentation:

找个人就嫁了吧 2024-10-12 19:20:53

如果将路径放在引号中,则不需要转义空格,但是当您调用 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 没有任何意义,因为目录 <代码>/cygdrive/c/Documents 不存在。

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.

柠檬 2024-10-12 19:20:53

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

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

再会!

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

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

Good Day!

染柒℉ 2024-10-12 19:20:53

现在可能已经很晚了,但我对这个问题使用了不同的解决方案
过去几年。在 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 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.

鸢与 2024-10-12 19:20:53

使用 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

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
失与倦" 2024-10-12 19:20:53

我是一个十足的cwywin菜鸟 - 但创建一个回到你的Windows主页的符号链接肯定是一个简单的第一步?

即:

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

那么你可以:

 ls wh

你可以为你的桌面等做同样的事情......并且不再担心 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:

 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

别靠近我心 2024-10-12 19:20:53

如果您定义正确,则使用变量的方法应该有效。您的内容无缘无故地包含反斜杠。

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

二手情话 2024-10-12 19:20:53

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文