如何导出Windows路径然后使用CD使用该变量?

发布于 2025-01-21 04:41:05 字数 675 浏览 0 评论 0原文

我通常会带有经常访问的目录/路径的导出变量,因此我不必键入整个内容(或选项卡)。例如,在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 技术交流群。

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

发布评论

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

评论(2

岁月流歌 2025-01-28 04:41:05

我对吉巴什的测试

# remove the trailing backslash
MY_LONG_PATH="C:\Users\name\OneDrive - Company, Inc\Documents\My Project Folder"   <---
# use quotes
$ ls -l "$MY_LONG_PATH"
total 0
drwxr-xr-x 1 user 197613 0 Apr 14 00:10 test/
$ cd "$MY_LONG_PATH"
$ pwd
/c/Users/name/OneDrive - Company, Inc/Documents/My Project Folder
$ ls
test/ 

My test on Gitbash

# remove the trailing backslash
MY_LONG_PATH="C:\Users\name\OneDrive - Company, Inc\Documents\My Project Folder"   <---
# use quotes
$ ls -l "$MY_LONG_PATH"
total 0
drwxr-xr-x 1 user 197613 0 Apr 14 00:10 test/
$ cd "$MY_LONG_PATH"
$ pwd
/c/Users/name/OneDrive - Company, Inc/Documents/My Project Folder
$ ls
test/ 
简美 2025-01-28 04:41:05

我想出了一种方法。虽然Ufopilot的答案很好,但我不喜欢使用具有特殊字符的变量的报价。另外,我发现当您使用Tab-Completion时,Bash(Bash-Completion)中会有一个错误,它将在命令行上的变量中逃脱$,从而破坏目录路径。因此,为了解决所有这些问题,我首先创建了一个软符号链接到包含Windows CMD空格的路径:

> mklink /D C:\Users\name\projects "C:\Users\name\OneDrive - Company, Inc\Documents\My Project Folder"

现在,我可以在.bashrc中导出变量:

export LONG_PATH="C:\Users\name\projects"

这允许我更改目录而无需在long_path周围放引号:

> cd $LONG_PATH

解决问题 :使用Tab-Completion,我不得不运行bash命令:

shopt -s direxpand

它的作用是首先扩展变量,然后在需要时逃脱特殊字符(例如,如果另一个目录具有空格)。一切都像我现在的其他路径变量一样工作!

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:

> mklink /D C:\Users\name\projects "C:\Users\name\OneDrive - Company, Inc\Documents\My Project Folder"

And now I can export the variable in .bashrc:

export LONG_PATH="C:\Users\name\projects"

This allows me to change directories without putting quotes around LONG_PATH:

> cd $LONG_PATH

To fix the problem with tab-completion, I had to run the bash command:

shopt -s direxpand

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!

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