相当于Unix shell`export`命令的PowerShell相当于什么?

时间:2020-06-10 16:40:08

标签: powershell shell

PowerShell中的等效项是什么:

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

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

例如,当我在zsh中运行时,我得到:

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

1 个答案:

答案 0 :(得分:2)

使用env:范围或驱动器:

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

xxx777xxx

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

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

hi
相关问题