PowerShell函数在Windows中运行,但在Linux中不运行的原因是什么?

huangapple go评论100阅读模式
英文:

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/

https://unix.stackexchange.com/questions/656005/what-does-case-sensitivity-is-a-function-of-the-linux-filesystem-not-the-linux

英文:

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/

https://unix.stackexchange.com/questions/656005/what-does-case-sensitivity-is-a-function-of-the-linux-filesystem-not-the-linux

huangapple
  • 本文由 发表于 2023年7月23日 22:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定