简体   繁体   中英

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

How does echo off and on working in the following script and what if we want to use echo off at some places of batch script let's say outside loops and echo on inside loops how should we do that

If I try to use @ instead of globally make it off by @echo off .Do I have to place '@' in every line?

Here the following code:

@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.

Here what the output looks

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

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

Please help in understanding the execution

The basic points first: you need to differentiate between echo a command when it is executed in a Batch file vs. the output of a command . The output of a command is what the command generates and it is always displayed on the screen (unless there is an output redirection, but that is another story).

The "echo of a command" is a trace of the command that will be executed. If "echo is on", the command itself will be displayed before it is executed.

If "echo is on" you may cancel the echo of a command if you precede it by @ .

You should note that the echo of commands happen when the commands are parsed , that is, reviewed before executed, and that several commands enclosed in parentheses are parsed as a whole. This means that changing the "echo" status inside a code block have not effect for the commands in the block: the echo will have the same status as when the block begins. Of course, if the echo status is changed in the block, it will work for the commands after the block.

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM