Windows OS

Batch File Echo and Echo Off: How to Control the Command Outputs in Batch Scripts

Using the “echo” command and the “@echo off” directive, we may manage the command outputs in batch programming on Windows.

Echo Command

The text is displayed on the screen by a batch file’s echo command:

Echo On

Any text that is present in a batch file is printed in the console window because the “echo” command is enabled by default. We can use the “echo” command followed by the message that we require to display:

echo Hello, Welcome to Batch Scripting!

 
This shows the console’s “echo” command and the “Hello, Welcome to Batch Scripting!” message. Open a command prompt, navigate to the working directory, and then execute the batch file there. This is the result:

Echo Off

However, we can use the “echo off” command to turn off the echoing so that no text will be displayed. To stop the commands in a batch file from being shown, the “echo off” command is frequently employed at the beginning of the file. This can be useful if we want to create a batch file that performs a series of tasks without displaying any output. It is important to note that “echo off” only stops the commands from printing, not the results.

Example:

The following batch file creates a new directory called “MyWorkingBackup” from which all the files in the current directory will be copied. It begins by creating the directory if it doesn’t exist yet:

@echo off
if not exist "MyWorkingBackup" mkdir "MyWorkingBackup"
copy * MyWorkingBackup

 
The “@echo off” command at the beginning of the file prevents the “copy” command from being displayed. This means that the only output from the batch file will be the “The files have been copied” message.

Creating a Backup of Files without Interrupting the User

We have four files in the working directory: two bat and two text files. These files are copied to “MyWorkingBackup” using the previous code:


Successful execution of the batch file:


 

Testing a New Command and Displaying the Output

The first line, “@echo on”, turns on the echo feature. This means that any text that is included in the batch file is displayed on the screen.

The next line, rem, the comment in this batch file, tests the new “dir” command. Despite not being carried out by the batch file, the comments are useful for describing what the batch file performs.

The command that is being tried is found on the third line which is dir. The current directory’s contents are listed with the “dir” command.

In line four, rem another observation is that the output of the “dir” command is displayed, thanks to the “echo on” command. This comment describes how the echo on command makes sure the dir command’s output is displayed.

The echo feature is enabled when this batch file is executed, displaying the “dir” command’s output.

@echo on
rem This batch file tests the new command "dir".
dir
rem The echo on command ensures that the output of the dir command is displayed.

 

Echo On/Off Toggle

Using the “echo on” and “echo off” commands, we can also turn the echo state on and off within a script.

Example:

The echo feature is turned off in the first line – @echo off. This implies that the user won’t see any text that is present in the batch file.

The second line enables the delay expansion. In other words, the variables are not enlarged until they are really used.

The third line, set /p “inputNumber=Enter a number:”, prompts the user to enter a number and assigns the value to the inputNumber variable.

The fourth line, if “%inputNumber%”==”0” (, checks if the value of the inputNumber variable is equal to 0. If it is, the code in the “if” statement is implemented.

The fifth line, echo Dividing by zero is not allowed, displays the “Dividing by zero is not allowed” message on the screen.

The sixth line, echo on, turns on the echo feature. This means that any text that is included in the “if” statement is displayed on the screen.

The seventh line, ) , marks the end of the “if” statement.

The eighth line, else ( , indicates that the code in the “else” statement is executed if the value of the inputNumber variable is not equal to 0.

The ninth line, set /a “calculationResult=10 / %inputNumber%”, sets the calculationResult variable to the value of 10 divided by the value of the inputNumber variable.

The tenth line, echo The result of dividing 10 by %inputNumber% is: !calculationResult!, displays the “The result of dividing 10 by %inputNumber% is: !” message on the screen. The exclamation mark (!) character is used to expand the value of the calculationResult variable.

The eleventh line, @echo off, turns off the echo feature. This means that any text that is included in the “else” statement will not be displayed on the screen.

The twelfth line, ECHO SUCCESS, displays the “SUCCESS” message on the screen.

The thirteenth line, endlocal, ends the local scope. This means that the variables that were defined in the setlocal statement will no longer be available.

The code that we provided is a simple batch file that prompts the user to enter a number and then displays the result of dividing 10 by the number. If the user enters a number equal to 0, the “Dividing by zero is not allowed” message is displayed. If not, the division’s outcome is shown.

@echo off
setlocal enabledelayedexpansion

set /p "inputNumber=Enter a number: "
if "%inputNumber%"=="0" (
    echo Dividing by zero is not allowed.
    echo on
) else (
    set /a "calculationResult=10 / %inputNumber%"
    echo The result of dividing 10 by %inputNumber% is: !calculationResult!    
    @echo off
)
ECHO SUCCESS
Endlocal

 

Redirecting the Output

A command’s output can also be directed in a certain direction. The > or >> operators allow us to direct it to a file instead of the terminal window, which is where it goes by default. For instance:

>: Redirects output and overwrites the target file if it exists.
echo Hello Americans!> output.txt
>>: Redirects output and appends it to the target file if it exists.
echo Hello, World! >> output.txt

 
This generates a file named “output.txt” which contains the “Hello Americans!” and “Hello, World! lines.

Conclusion

In batch scripting, the “echo” command is used to display the messages or the results of other commands in the console window, while “@echo off” is a directive that turns off the display of subsequent commands in the script. By default, the command outputs are shown in the console, but “@echo off” is often used at the beginning of a script to make it cleaner by hiding the commands as they are executed. You can also toggle the echo state on and off within a script to control the visibility of the command outputs selectively. This combination of the echo and @echo off commands provides flexibility in managing the presentation of information and results within the batch scripts.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content