如何导出Windows路径然后使用CD使用该变量?
我通常会带有经常访问的目录/路径的导出变量,因此我不必键入整个内容(或选项卡)。例如,在Windows中,我将创建一个环境变量,然后在bash提示(git bash)中使用它:cd $ my_long_path
或,我可以在.bashrc中导出路径并使用它同样的方式。两种方式都可以正常工作,直到将空间添加到混音中为止。例如,我希望能够导出一个路径,例如c:\ users \ name \ oneDrive -oneDrive -company,inc \ documents \我的项目文件夹\
。您可能会说,只需使用其他目录即可。不幸的是,我无法更改它,因为它是由我的组织控制的。因此,考虑到我不能更改它,我如何才能使其与环境或导出变量一起使用?我尝试过两种方式,但似乎无法正确。我已经在.bashrc c \:/users/name/oneedrive \ - \ company,\ inc/documents/my \ project \ folder
中逃脱了所有字符。当我输入(终端)时,这将在Bash CLI上使用,但是当我将其导出为变量时不起作用。我已经尝试在整个过程中使用引号,但这也不起作用。我已经在常规的Windows环境变量控制面板中尝试过,但这无效。有人知道修复程序吗?
同样,目标只是能够:cd $ long_path
带有带有空格的Windows目录的变量
I typically export variables with directories/paths that I frequently visit so that I don't have to type the whole thing (or tab). For example, in Windows, I'll create an environment variable and then use it in a bash prompt (git bash): cd $MY_LONG_PATH
Or, I can export a path in my .bashrc and use it the same way. Both ways work fine until you add spaces into the mix. For example, I'd like to be able to export a path like C:\Users\name\OneDrive - Company, Inc\Documents\My Project Folder\
. You might say, just use a different directory. Unfortunately, I can't change this one because it's controled by my org. So, considering I can't change it, how can I get it to work with an environment or exported variable? I've tried both ways, but can't seem to get it right. I've escaped all the chars in .bashrc c\:/Users/name/OneDrive\ -\ Company,\ Inc/Documents/My\ Project\ Folder
. This will work on the bash cli when I type it in (terminal), but doesn't work when I export it as a variable. I've tried using quotes around the whole thing and that doesn't work either. I've tried it in the regular windows environment variable control panel, and that doesn't work. Does anyone know the fix, if there is one?
Again, the goal is just to be able to: cd $LONG_PATH
with the variable containing a windows directory with spaces
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对吉巴什的测试
My test on Gitbash
我想出了一种方法。虽然Ufopilot的答案很好,但我不喜欢使用具有特殊字符的变量的报价。另外,我发现当您使用Tab-Completion时,Bash(Bash-Completion)中会有一个错误,它将在命令行上的变量中逃脱$,从而破坏目录路径。因此,为了解决所有这些问题,我首先创建了一个软符号链接到包含Windows CMD空格的路径:
现在,我可以在.bashrc中导出变量:
这允许我更改目录而无需在long_path周围放引号:
解决问题 :使用Tab-Completion,我不得不运行bash命令:
它的作用是首先扩展变量,然后在需要时逃脱特殊字符(例如,如果另一个目录具有空格)。一切都像我现在的其他路径变量一样工作!
I figured out a way to do this. While ufopilot's answer was good, I didn't like having to use quotes for variables that have special characters. Also, I found that when you use tab-completion, there's a bug in bash (bash-completion) that will escape the $ in a variable on the command-line, which in turn would break the directory path. So to fix all of this, I first created a soft symlink to the path that contains spaces using Windows cmd:
And now I can export the variable in .bashrc:
This allows me to change directories without putting quotes around LONG_PATH:
To fix the problem with tab-completion, I had to run the bash command:
What this does is it expands the variable first and then escapes the special characters if needed (for example, if yet another directory has spaces). Everything works like my other path variables now!