gpt4 book ai didi

powershell - PowerShell与Unix shell `export`命令等效吗?

转载 作者:行者123 更新时间:2023-12-03 01:26:39 25 4
gpt4 key购买 nike

在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,因为它更加跨平台友好,并且自上而下比自下而上更容易,因为这意味着困难的小众脚本可以留在最后。

最佳答案

使用env:范围或驱动器:

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

xxx777xxx

使用env:作为驱动器示例:
set-content env:foo 'hi'; bash -c 'echo $foo'                    

hi

关于powershell - PowerShell与Unix shell `export`命令等效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62308709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com