英文:
Why does PowerShell function work in Windows but not Linux?
问题
我编写了一些PowerShell实用函数,用于在控制台上将PATH和类似PATH的环境变量的组件分别显示在不同行上。它们在Windows中运行正常,但在Linux中会产生意外结果。
我将以下函数放在了每个系统上的$PROFILE
文件中。
Function Split-EnvPath {
param ($EnvPath)
$EnvPath -split [IO.Path]::PathSeparator
}
Function Show-Path {
Split-EnvPath $env:Path
}
Function Show-PSPath {
Split-EnvPath $env:PSModulePath
}
所有三个函数在Windows中都可以正常工作,但在Linux中,Show-Path
只会显示一个空行;在Linux PowerShell命令行中输入 Split-EnvPath $env:Path
会产生你所期望的结果。我发现 $env:Path
的值是 null
,但我无法猜出原因。我猜想可能是脚本访问环境时出了问题,但为什么 Show-PSPath
没有相同的问题呢?
英文:
I wrote a couple of PowerShell utility functions to display the components of PATH and PATH-like environment variables to the console on separate lines. They work in Windows, but have unexpected results in Linux.
I put the following functions in my $PROFILE
file on each system.
Function Split-EnvPath {
param ($EnvPath)
$EnvPath -split [IO.Path]::PathSeparator
}
Function Show-Path {
Split-EnvPath $env:Path
}
Function Show-PSPath {
Split-EnvPath $env:PSModulePath
}
All three functions work in Windows, but Show-Path
doesn't show anything but a blank line on Linux; Typing Split-EnvPath $env:Path
in the Linux PowerShell command line does what you'd expect it to. I figured out that the value of $env:Path
is null
, but I can't guess why. I assume there's something wrong with the script accessing the environment, but why wouldn't Show-PSPath
have the same issue?
答案1
得分: 3
Linux是一个区分大小写的操作系统。在这里,"PATH"和"Path"是不同的。
所以,不要使用"Path",而是使用"PATH"。
尝试以下命令:
$env:PATH
在此处阅读更多关于Linux区分大小写的信息:
https://www.linuxquestions.org/questions/linux-general-1/why-is-linux-case-sensitive-125995/
英文:
Linux is a case-sensitive Operating System. Here "PATH" and "Path" are different.
So instead of "Path", use "PATH".
Try the below command:
$env:PATH
Read more about Linux case sensitivity here:
https://www.linuxquestions.org/questions/linux-general-1/why-is-linux-case-sensitive-125995/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论