【问题标题】:How to set environment variable and execute command in one line (PowerShell)?如何在一行中设置环境变量并执行命令(PowerShell)?
【发布时间】:2018-06-18 05:46:22
【问题描述】:

如何在PowerShell中设置环境变量的值并在一行中执行命令?

我有这个:

PGPASSWORD=db_pass psql -U db_user -d db_name -a -h localhost -f some.sql

它在 Linux 中运行良好。

如何将上面的内容转换为适用于 Windows Server 2012 - Windows 10 的 PowerShell 命令?

我试过了:

SET "PGPASSWORD=db_pass" & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql

我收到错误:“& 保留供将来使用...” - Windows 2012。

【问题讨论】:

    标签: bash powershell environment-variables


    【解决方案1】:

    试试这个:

    $env:PGPASSWORD='db_pass'; & "C:\Program Files (x86)\PostgreSQL\9.4\bin\psql.exe" -U postgres -a -d db_name -h localhost -f some.sql
    

    实际上这是用分号分隔的两个命令,因此它们可以作为一行运行。

    另请注意,您可以在 PowerShell 中通过 $env:<name of var> 设置环境变量。

    【讨论】:

    • 实际上,这并不完全等同于问题所在的 bash 命令,因为它在调用命令后将环境变量保留在原位。在 bash 命令中,PGPASSWORD 在调用 psql 后消失。
    猜你喜欢
    • 2019-06-30
    • 2019-01-08
    • 1970-01-01
    • 2014-06-01
    • 2021-12-25
    • 2011-04-04
    • 2014-06-04
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多