将 cygwin bin 目录添加到 powershell 路径环境变量 ... powershell 中的 cygwin
Powershell 非常适合编写脚本。但当涉及到日常使用时,某些东西可能会成为一个巨大的皮塔饼!
所以我想如果我能在我的 profile.ps1 中做这样的事情那就太好了:
$env:path = "$($env:path);c:\cygwin\bin"
访问 tar、zip 等实用程序...但这不起作用。当我这样做时,变量看起来是正确的:
PS > $env:path
但是当我尝试这样做时,比如说,
PS > unzip foo.zip
我收到命令未找到类型错误。
WTF PowerShell!?
编辑:很好的答案!今天早上我用新的眼光看待它,意识到我只需要正确拼写“cygwin”即可!现在我不必在两个控制台之间来回切换。使用此技巧的任何人都应该注意,您在 powershell 中的路径是按顺序评估的 - 如果您将 c:\cygwin\bin 放在 $env:path 变量的末尾,它将被最后搜索,所以它不会'不会干扰现有的 powershell 别名/cmdlet。
Powershell is great for scripting. But when it comes to everyday use, certain things can be a huge PITA!!
so i thought it would be great if i could do something like this in my profile.ps1:
$env:path = "$($env:path);c:\cygwin\bin"
to get access to utilities like tar, zip, etc... but this doesn't work. The variable looks right when i do:
PS > $env:path
but when i try to do, say,
PS > unzip foo.zip
i get a command not found type error.
WTF PowerShell!?
edit: great answers! I looked at it with fresh eyes this morning and realized that I just needed to spell 'cygwin' correctly! now I don't have to switch back and forth between two consoles. It should be noted for anyone who uses this tip that your path in powershell is evaluated in order - if you put c:\cygwin\bin at the end of the $env:path variable, it will be searched last, so it won't interfere with existing powershell aliases / cmdlets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它对我有用:
设置您的个人资料:
或者只是当前的外壳:
It worked for me:
To set your profile:
Or just the current shell:
只要您尝试在同一 Powershell 会话中使用 cygwin bin,就可以使用 $env:path 将 cygwin bin 添加到 PATH。如果您打开新控制台或者关闭并打开 Powershell,它将不会被保留。否则,你正在做的事情应该有效。确保您确实添加了正确的路径。如果您想保留更改,请将该行添加到您的
$profile
中。另外,尝试使用 Mingw / Msys / Msysgit utils。我发现 Mingw 比 cygwin 更轻量(如果您使用 cygwin 只是为了获取其中一些实用程序。)
Using
$env:path
to add the cygwin bin to PATH should work as long as you are trying to use it in the same Powershell session. If you open a new console or if you close and open Powershell, it will not be persisted. Otherwise, what you are doing should work. Make sure you are indeed adding the correct path. If you want to persist the changes, add the line to your$profile
.Also, try using the Mingw / Msys / Msysgit utils. I find Mingw to be more lightweight than cygwin ( if you are using cygwin just to get some of these utils.)
默认情况下,PowerShell 仅会修改其 PATH 的本地副本。当您运行外部命令时,它们不会看到本地环境变量。
根据这篇 TechNet 文章,您可以回退到 .NET 静态方法如果您希望这是永久更改,请在用户级别设置环境变量来执行此操作:
PowerShell by default is only going to modify its local copy of PATH. When you run an external command, they aren't going to see the local environment variables.
Per this TechNet article, you can fall back to the .NET static method SetEnvironmentVariable to do this at the user level if you want this to be a permanent change: