如何在 NPM 脚本中使用 Windows console 'set' 变量?
How to use Windows console 'set' variables with NPM scripts?
这在 Windows 控制台中按预期工作:
set A="qwerty" && echo %A%
输出:"qwerty"
但是当我尝试 运行 NPM scipts 中的相同命令时:
package.json:
"scripts": {
"qwerty": "set A=\"qwerty\" && echo %A%"
}
> npm run qwerty
输出为:%A%
我是做错了什么,还是 NPM 运行 不应该这样工作?
到目前为止,我发现这些命令必须在不同的脚本中才能正常工作,并且 运行 必须按照特定的顺序。所以,这是它的工作方式:
"scripts": {
"aaa": "set TMP=test && npm run bbb",
"bbb": "echo %TMP%"
}
npm run aaa
输出:
test
但是这个不行:
"scripts": {
"aaa": "set TMP=test",
"bbb": "npm run aaa && echo %TMP%"
}
npm run bbb
输出:<just empty>
看起来需要两个单独的 npm run
命令来查找创建的变量:变量应该在第一个 npm run
中创建,并且可以在第二个中找到。
您的示例 set A="qwerty" && echo %A%
不正确。 cmd
提示符/批处理文件中的变量每行/命令扩展一次:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && echo %A%
%A%
==> echo %A%
"qwerty"
Why this behaviour?
The SET
command was first introduced with MS-DOS 2.0 in March 1983,
at that time memory and CPU were very limited and the expansion of
variables once per line was enough.
使用 CALL
command 的解决方法:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && CALL echo %A%
"qwerty"
编辑:
为了完整起见,以下批处理脚本显示了百分比扩展的机制及其与CALL
命令的组合详细(注意加倍%
批处理文件中的百分号 CALL Echo %%_var%%
):
@ECHO OFF
SETLOCAL
if NOT "%~1"=="" ECHO ON
echo 1st:
Set "_var=first"
Set "_var=second" & Echo %_var% & CALL Echo %%_var%%
echo 2nd:
Set "_var=first"
Set "_var=second" & CALL Echo %%_var%% & Echo %_var%
输出,echo OFF
:
==> D:\bat\SO237418.bat
1st:
first
second
2nd:
second
first
输出,echo ON
:
==> D:\bat\SO237418.bat on
==> echo 1st:
1st:
==> Set "_var=first"
==> Set "_var=second" & Echo first & CALL Echo %_var%
first
second
==> echo 2nd:
2nd:
==> Set "_var=first"
==> Set "_var=second" & CALL Echo %_var% & Echo first
second
first
这在 Windows 控制台中按预期工作:
set A="qwerty" && echo %A%
输出:"qwerty"
但是当我尝试 运行 NPM scipts 中的相同命令时:
package.json:
"scripts": {
"qwerty": "set A=\"qwerty\" && echo %A%"
}
> npm run qwerty
输出为:%A%
我是做错了什么,还是 NPM 运行 不应该这样工作?
到目前为止,我发现这些命令必须在不同的脚本中才能正常工作,并且 运行 必须按照特定的顺序。所以,这是它的工作方式:
"scripts": {
"aaa": "set TMP=test && npm run bbb",
"bbb": "echo %TMP%"
}
npm run aaa
输出:
test
但是这个不行:
"scripts": {
"aaa": "set TMP=test",
"bbb": "npm run aaa && echo %TMP%"
}
npm run bbb
输出:<just empty>
看起来需要两个单独的 npm run
命令来查找创建的变量:变量应该在第一个 npm run
中创建,并且可以在第二个中找到。
您的示例 set A="qwerty" && echo %A%
不正确。 cmd
提示符/批处理文件中的变量每行/命令扩展一次:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && echo %A%
%A%
==> echo %A%
"qwerty"
Why this behaviour?
The
SET
command was first introduced with MS-DOS 2.0 in March 1983, at that time memory and CPU were very limited and the expansion of variables once per line was enough.
使用 CALL
command 的解决方法:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && CALL echo %A%
"qwerty"
编辑:
为了完整起见,以下批处理脚本显示了百分比扩展的机制及其与CALL
命令的组合详细(注意加倍%
批处理文件中的百分号 CALL Echo %%_var%%
):
@ECHO OFF
SETLOCAL
if NOT "%~1"=="" ECHO ON
echo 1st:
Set "_var=first"
Set "_var=second" & Echo %_var% & CALL Echo %%_var%%
echo 2nd:
Set "_var=first"
Set "_var=second" & CALL Echo %%_var%% & Echo %_var%
输出,echo OFF
:
==> D:\bat\SO237418.bat
1st:
first
second
2nd:
second
first
输出,echo ON
:
==> D:\bat\SO237418.bat on
==> echo 1st:
1st:
==> Set "_var=first"
==> Set "_var=second" & Echo first & CALL Echo %_var%
first
second
==> echo 2nd:
2nd:
==> Set "_var=first"
==> Set "_var=second" & CALL Echo %_var% & Echo first
second
first