Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop my batch file from printing the code it is running?

I have the following script:

FOR %%i IN (1 2 3) DO (     
    IF %%i==1 ( 
       ECHO %%i 
    )
    IF %%i==2 ( 
       ECHO %%i 
    )
    IF %%i==3 ( 
       ECHO %%i 
    )
)

I just wanted to print

1
2
3

because I will be using the same logic again to write a more complete task ... I'm not a Windows guy, and I have no idea how to do it in batch. Instead I'm getting:

c:\>FOR %i IN (1 2 3) DO (
IF %i == 1 (ECHO %i  )
 IF %i == 2 (ECHO %i  )
 IF %i == 3 (ECHO %i  )
)

c:\>(
IF 1 == 1 (ECHO 1  )
 IF 1 == 2 (ECHO 1  )
 IF 1 == 3 (ECHO 1  )
)
1

c:\>(
IF 2 == 1 (ECHO 2  )
 IF 2 == 2 (ECHO 2  )
 IF 2 == 3 (ECHO 2  )
)
2

c:\>(
IF 3 == 1 (ECHO 3  )
 IF 3 == 2 (ECHO 3  )
 IF 3 == 3 (ECHO 3  )
)
3
like image 722
cybertextron Avatar asked Oct 03 '13 00:10

cybertextron


People also ask

How do I stop a batch file from looping?

The only way to stop an infinitely loop in Windows Batch Script is by either pressing Ctrl + C or by closing the program.

How do I keep CMD from running after batch file?

Edit your bat file by right clicking on it and select “Edit” from the list. Your file will open in notepad. Now add “PAUSE” word at the end of your bat file. This will keep the Command Prompt window open until you do not press any key.

What is @echo off in batch script?

batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

Can you edit a batch file while it running?

Short answer: yes, batch files can modify themselves whilst running.


1 Answers

To avoid echoing the Windows commands in a batch file, use @echo off:

@ECHO OFF
FOR %%i IN (1 2 3) DO (     
    IF %%i==1 ( 
       ECHO %%i 
    )
    IF %%i==2 ( 
       ECHO %%i 
    )
    IF %%i==3 ( 
       ECHO %%i 
    )
)

Note the preceding @ in echo off prevents echo off from echoing. If you don't have the @ then you'll see echo off echoed to the terminal but then echoing will be off after that point. A batch file command which is preceded by @ is not echoed. So @ can be used to prevent echoing of individual commands.

like image 161
lurker Avatar answered Oct 08 '22 07:10

lurker