The echo Command in Batch

Siddharth Bishnoi Jul 01, 2022
  1. Use the echo Command in Batch
  2. Use the echo Command to Print Message
  3. echo a New Line or Blank Line in a Batch File
  4. echo Text Into a File
  5. Use the echo Command to Echo a Variable
  6. Echo the System Path
  7. Use the echo Command With Escape Command Characters
  8. Conclusion
The echo Command in Batch

In Windows, the echo command displays messages on the command line. Apart from Command shell and PowerShell, it is available in many other operating system shells such as DOS, OS/2, ReactOS, and other Unix and Unix-like operating systems.

Nowadays, it is mainly used in shell scripts and batch files. Let’s discuss Batch’s echo command and its options.

Use the echo Command in Batch

The echo command is an internal command used to print out the text on the screen or turn on or off the command-echoing. It does not set the errorlevel or clear the errorlevel.

Every command executed on running a batch file is shown on the command line along with the output. When we run a batch file, all the commands are executed line by line in a sequential manner.

Using the echo command, we can turn off the echoing of the commands in the command prompt even with the echo command itself.

Syntax:

echo [on | off]
echo <message>
echo /?
  • on - display commands along with the output.
  • off - hides the commands and only displays the output.
  • message - text to be printed.

If you type echo without any parameters or options, it will show the status of the echo command(i.e., on or off). In Batch files, it is mostly used to turn off the command’s echoing and only show the output, and the echo on command is useful while debugging a batch file.

To check the status of the echo command or echo setting, execute the following command in the command prompt.

echo

Output:

check status of echo

To turn off the echoing of the command, add echo off at the top of your Batch file.

echo off
ipconfig
PAUSE

echo off

Output:

echo off output

However, the above command will show the line echo off in the command prompt. To hide this line, add @ at the start of the echo off.

@echo off
ipconfig
PAUSE

turn off echoing along with echo command

Output:

turn off echoing along with echo command - output

Use the echo Command to Print Message

The echo command can also display messages while executing commands in a batch file.

@echo off
echo hello world
echo this is an example of how to print a message using the echo command.
cmd /k

print a message using echo command

Output:

print a message using echo command - output

You can also use both echo on and echo off in the same batch file. Sometimes, you may need to turn off the echoing for some commands and turn on the echoing for other commands.

In that case, you can use both echo on and echo off, as shown in the following example.

@echo off
echo hello world
echo Here, the echoing is turned off i.e. no commands are shown.

@echo on
echo Hello
echo Here, the echoing is on i.e. commands are shown along with the output
cmd /k

echo on and off in same batch file

Output:

echo on and off in same batch file - output

echo a New Line or Blank Line in a Batch File

To echo a new line or display a message in a separate line in a batch file, use echo. or echo: between the commands to add a new line. The output for both the commands will be the same.

Example - 1:

@echo off
echo hello
echo:
echo world
echo this will add a blank line between hello and world
cmd /k

add a blank line using echo

Example - 2:

@echo off
echo hello
echo.
echo world
echo this will add a blank line between hello and world
cmd /k

add a blank line using echo - method 2

Output:

add a blank line using echo - output

echo Text Into a File

To echo a message into a text file, execute the following command.

echo this line will be saved to a text file > testfile.txt

echo text to a file

You can also create an empty file using the echo command. To create an empty file, execute the following command.

echo. 2>testfile.txt

create an empty file using echo command

Use the echo Command to Echo a Variable

To echo a variable using the echo command, execute the following command:

ECHO %OS%
cmd /k

echo a variable

It’s recommended to use echo: instead of space as given below though it has some limitations. However, the output will be the same for both.

ECHO:%OS%
cmd /k

It is because if you use space, it may turn out to execute the command as echo on or echo off instead of displaying the variable’s value. As shown in the example below, if the value of the variable is set to off, using space will take it as echo off and turn off the echoing.

Instead, if we use echo: it will just display the value of the variable.

Example - 1:

Set test=OFF
Echo %test%
echo echo is turned off instead of displaying the value
cmd /k

echo a set variable

Output:

echo a set variable - output

Example - 2:

Set test=OFF
Echo:%test%
echo the value is displayed

echo a set variable - method 2

Output:

echo a set variable method 2 - output

Echo the System Path

To echo each item in the path in a new line, execute the following command.

ECHO:%PATH:;= & ECHO:%

echo system path

Output:

echo system path - output

Use the echo Command With Escape Command Characters

When you use the echo command along with the command characters, like the redirection(&) and pipe(|, on, off) characters, the command character will by default precede the echo command. To avoid this, we need to use the escape characters(:, ^) whenever the command characters are used.

Add the escape character before the command character for each command character separately.

@echo off
ECHO Here, the escape character is used for the ^& character like Mickey ^& Mouse
ECHO file1 ^| file2 ^| file3

escape command characters using echo command

Output:

escape command characters using echo command - output

Conclusion

We have discussed the echo command and covered almost everything, along with examples. It is a very useful command used in Batch files for various reasons, as explained in the examples above.

Related Article - Batch Command