echo off 和 on 如何在同一个批处理脚本中执行?

How does echo off and on are executes in same batch script?

如何在以下脚本中关闭和打开回显以及如果我们想在批处理脚本的某些地方使用 echo off 怎么办让我们说外部循环和 echo on 内部循环我们应该怎么做

如果我尝试使用 @ 而不是全局使用 @echo off。我是否必须在每一行中放置“@”?

这里是下面的代码:

@echo off
set select=package:com.google.ar.core
set "str=%select%"
set "string1=%str::=" & set "string2=%"     

@echo on
echo %select%
echo %string2%
@echo off
echo.

这是输出结果

echo package:com.google.ar.core
package:com.google.ar.core

echo com.google.ar.core
com.google.ar.core

请帮助理解执行

首先是基本点:您需要区分 回显命令 在批处理文件中执行时与 命令的输出。命令的输出是命令 生成的 并且它始终显示在屏幕上(除非有输出重定向,但那是另一回事)。

"echo of a command" 是将要执行的命令的 trace。如果"echo is on",命令本身将在执行前显示。

如果 "echo is on" 您可以 取消 如果您在命令之前加上 @.

命令的回显

需要注意的是,命令的回显发生在解析的时候,也就是在执行前经过审核,括号中的几个命令是作为一个整体解析的。这意味着在代码块 中更改 "echo" 状态 对块中的命令没有影响:回显将具有与块开始时相同的状态。当然,如果回显状态在块中发生变化,它将对块之后的命令起作用。

@echo off
rem "Echo" is off here; commands are not echoed
rem This command does not generate any output
echo This command generate output

echo on
rem "Echo" is on here; commands are echoed
rem This command does not generate any output
echo This command generate output

@rem This command is preceded by "@" so it is not echoed
@echo This command is also not echoed, but produce an output


@echo off
rem "Echo" is off here; commands are not echoed

rem The block below is *parsed* entirely before executed
(
   echo This command produces output
   echo on
   rem Previous command set "Echo" on, but this block was parsed already
   rem so the commands placed here are *not* echoed either
   echo Command that produce output
)

rem The echo is on here