首页 > 解决方案 > Unix shell `export` 命令的 PowerShell 等效项是什么?

问题描述

PowerShell 中的等价物是什么:

export pwtst=777; bash -c 'echo xxx${pwtst}xxx'

?

我想将 PowerShell 中设置的变量公开给传统的 Unix shell。

例如,当我运行 this is inzsh时,我得到:

xxx777xxx

但在 PowerShell

export $pwtst=777; bash -c 'echo xxx${pwtst}xxx'

结果是

export: The term 'export' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我试过了

$global:pwtst='777'; bash -c 'echo xxx${pwtst}xxx'

但这又回来了

xxxxxx

我试过了

$global:pwtst=777; Export-ModuleMember -Variable pwtst; bash -c 'echo xxx${pwtst}xxx'

这导致

Export-ModuleMember: The Export-ModuleMember cmdlet can only be called from inside a module.

PS-我正在尝试将值传递给子外壳,这对我来说只是一个测试。我知道我可以做到bash -c "echo xxx${pwtst}xxx",但随后将是 PowerShell 进行变量替换,而不是子 shell(即 Bash)。我的用例是将现有的 shell 脚本集合移植到 PowerShell,因为它对跨平台更友好,而且自上而下比自下而上更容易,因为这意味着可以将困难的小众脚本留到最后。

标签: powershellshell

解决方案


使用env:示波器或驱动器:

$env:pwtst='777'; bash -c 'echo xxx${pwtst}xxx'   

xxx777xxx

使用 env: 作为驱动器示例:

set-content env:foo 'hi'; bash -c 'echo $foo'                    

hi

推荐阅读