批处理脚本中的“2^>^&1”是什么意思?

What does "2^>^&1" mean in batch script?

我正在学习批处理脚本。我正在审查 GitHub 上的 getJavaVersion.bat 脚本。我明白了以下代码行中的 2^>^&1 表达式的用途。但我无法理解这种语法 (2^>^&1) 是如何使用的。你能帮我解决这个问题吗?

for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"

下面的命令显示生成的值:

for /f "delims=" %%i in ('java -fullversion') do set output=%%i
echo %output%
:: OUTPUT >> java full version "18.0.1.1+2-6"

for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
echo %jver%
:: OUTPUT >> 18011+2

命令 returns the output at the STDERR stream (handle 2) rather than at the STDOUT stream (handle 1). for /F, together with a command behind the in keyword, captures and parses the command output at the STDOUT stream. To capture the STDERR stream you need to redirect it by 2>&1, meaning that handle 2 (STDERR) is redirected where handle 1 points to (STDOUT). To ensure that the redirection is not applied to the for /F command itself, you need to properly escape it (^) 是为了让特殊字符 >& 失去它们的特殊含义,直到整个 for /F 命令行被处理。要捕获的命令最终以非转义方式包含重定向表达式。