powershell - 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,因为它对跨平台更友好,而且自上而下比自下而上更容易,因为这意味着可以将困难的小众脚本留到最后。
解决方案
使用env:
示波器或驱动器:
$env:pwtst='777'; bash -c 'echo xxx${pwtst}xxx'
xxx777xxx
使用 env: 作为驱动器示例:
set-content env:foo 'hi'; bash -c 'echo $foo'
hi
推荐阅读
- selenium - 将经过身份验证的会话从 selenium 传递到 scrapy
- android - BottomSheetDialogFragment.viewBinding
- rabbitmq - 微服务世界中的 RabbitMQ 交换
- java - 将吊销数据添加到 CMS 容器
- javascript - React onChange 事件不会在自定义输入上触发
- php - PHP,刀片合并文件。拉拉维尔
- java - 如何使用 Spring Data JPA 访问 Azure AD 集成 Postgres DB?
- django - 单个对象解析器的 DjangoListField 类比
- javascript - NodeJS - 2 个进程中的一个类/模块实例
- python - 我是 jupyter 的新手,不明白这个错误,我正在寻找删除 $ 符号