Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从cygwin传播powershell运行的windows环境变量_Windows_Bash_Powershell_Cygwin - Fatal编程技术网

如何从cygwin传播powershell运行的windows环境变量

如何从cygwin传播powershell运行的windows环境变量,windows,bash,powershell,cygwin,Windows,Bash,Powershell,Cygwin,我已经安装了cygwin,即将执行在powershell中编写的脚本。我是这样做的: powershell "& ""C:\my\script.ps1""" 正如预期的那样,我必须这样做,因为在该脚本中,我正在执行另一个外部命令,等等 我想在脚本中添加一些环境变量,所以我想编写如下代码 powershell "$FOO="bar"; & ""C:\my\script.ps1""" 因此,我可以访问脚本中的$FOO变量并对其进行处理。这个想法是,如果这个变量没有定义,我就使用一

我已经安装了cygwin,即将执行在powershell中编写的脚本。我是这样做的:

powershell "& ""C:\my\script.ps1"""
正如预期的那样,我必须这样做,因为在该脚本中,我正在执行另一个外部命令,等等

我想在脚本中添加一些环境变量,所以我想编写如下代码

powershell "$FOO="bar"; & ""C:\my\script.ps1"""
因此,我可以访问脚本中的
$FOO
变量并对其进行处理。这个想法是,如果这个变量没有定义,我就使用一些默认值。我知道这也可以通过一些环境变量来实现,或者我可以把这些变量放到概要文件(profile.ps1)中,但是我想去掉那个文件,所以我不需要任何文件,我可以用我显示的变量覆盖默认值

但他说:

=bar : The term '=bar' 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.
At line:1 char:1
所以我在想,类似这样的事情可以奏效:

powershell { $web = "google.com" ; & ping.exe $web }
但它只在powershell控制台中工作,不在cygwin中工作,cygwin说

-bash: syntax error near unexpected token `&'
因此,
&
似乎被视为bash字符。我千方百计想逃避它

bash -c "'powershell { \$web = \"google.com\" ; & ping.exe \$web }'"
但这是输出

bash: powershell { $web = "google.com" ; & ping.exe $web }: command not found
谢谢你的提示

更新

我能够做到这一点:

 powershell "Invoke-Command -ScriptBlock {\$env:FOO = \"google.com\" ; & ""C:\my\script.ps1"" }"

但是,当我试图通过
$env:FOO
访问
FOO
变量时,它是空的,似乎我无法这样做,因为该脚本正在另一个作用域中运行,或者

此命令将环境变量从cygwin(
$FOO=“bar”
)传递到powershell,然后运行另一个powershell命令(
dir env:
),该命令列出了环境变量(证明已设置):

用脚本调用替换
dir env:
部分,这应该对您有用

编辑:以下是命令(引用方式略有不同),包括对外部脚本的调用:

powershell '$env:FOO="bar";& "C:\script name.ps1"'

此命令将环境变量从cygwin(
$FOO=“bar”
)传递到powershell,然后运行另一个powershell命令(
dir env:
),该命令列出了环境变量(证明已设置):

用脚本调用替换
dir env:
部分,这应该对您有用

编辑:以下是命令(引用方式略有不同),包括对外部脚本的调用:

powershell '$env:FOO="bar";& "C:\script name.ps1"'