PowerShell:仅为单个命令设置环境变量 [英] PowerShell: Setting an environment variable for a single command only

查看:112
本文介绍了PowerShell:仅为单个命令设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上,我可以这样做:

On Linux, I can do:

$ FOO=BAR ./myscript

在设置了环境变量FOO的情况下调用"myscript".

to call "myscript" with the environment variable FOO being set.

在PowerShell中是否有类似的可能,即不必先设置变量,调用命令然后再次取消设置变量?

Is something similar possible in PowerShell, i.e. without having to first set the variable, call the command, and then unset the variable again?

要更加清楚我的用例-我不想将其用作脚本的一部分.相反,我有一个第三方脚本,可以使用环境变量来控制其行为,但是在这种情况下,不能使用命令行参数.因此能够在输入之间交替

To be more clear about my use case - I don't want to use this as part of a script. Rather, I have a third-party script whose behavior I can control using environment variables, but, in this case, not command line arguments. So being able to alternate between typing

$ OPTION=1 ./myscript

$ ./myscript

会非常方便.

推荐答案

通常,最好通过参数而不是通过参数将信息传递给脚本. 全局(环境)变量.但是,如果这是您需要执行的操作,则可以通过以下方式进行:

Generally, it would be better to pass info to the script via a parameter rather than a global (environment) variable. But if that is what you need to do you can do it this way:

$env:FOO = 'BAR'; ./myscript

以后可以删除环境变量$ env:FOO,如下所示:

The environment variable $env:FOO can be deleted later like so:

Remove-Item Env:\FOO

这篇关于PowerShell:仅为单个命令设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆