echo - Display Text

Maintained on

What is the echo command?

There are mainly two ways to use the echo command:

  • Display text
  • Control the display of commands

The most common use is the first one, which is to display text on the command prompt.

By using the echo command, you can display any text on the command prompt.

The second use is to control the display of commands. Normally, when a command is executed on the command prompt, it is displayed on the screen. However, by using the echo command, you can control the display of the executed command.

In addition, the first use of the echo command is sometimes used to create an empty file.

This page provides clear explanations and specific sample code for the basic usage and setting options of the echo command.

Basic usage of the echo command

The echo command is a command for displaying the specified string on the screen.

Let’s start with a simple usage and gradually look at options and applications.

Basic usage on the command prompt

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>echo Hello, World!

When you execute this command, the text “Hello, World!” that you entered after the echo command will be output.

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>echo Hello, World!
Hello, World!
C:\users\user>

Basic usage in a batch file

@echo off
setlocal

echo Hello, World!

endlocal
exit

When you run this batch file, “Hello, World!” will be displayed just like in the command prompt.

About “@echo off”

The echo command has several options.

echo on and echo off

“echo on” and “echo off” are options that control whether the command itself is displayed when the command prompt or batch file is executed.

If you specify “echo on”, the command itself will be displayed when the command is executed. In the previous example, “echo Hello, World!” was displayed, which is the effect of “echo on”.

By default, “echo on” is enabled.

If you specify “echo off”, the command itself will not be displayed when the command is executed, and only the result will be displayed.

In batch files, it is common to suppress the display of commands by specifying “@echo off” at the beginning.

Displaying a blank line

You can display a blank line by using “echo.”.

How to use in the command prompt

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>echo.

How to use in a batch file

@echo off
setlocal

echo Hello, World!
echo.
echo This is a new line.

endlocal
exit

Advanced usage of the echo command

The echo command can also be used to display environment variables and calculation results.

Displaying environment variables

To display environment variables, specify them in the format %variable name%.

Usage on the command prompt

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>echo %USERNAME%

When you execute this command, your username will be displayed.

Usage in a batch file

@echo off
setlocal

echo Your username is %USERNAME%.

endlocal
exit

When you run this batch file, your username will be displayed.

Displaying calculation results

You can perform calculations using the set /a command and display the result using the echo command.

Usage on the command prompt

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>set /a result=3+4

When you execute this command, the calculation result of 7 will be displayed.

Usage in a batch file

@echo off
setlocal

set /a result=3+4
echo The result is %result%.

endlocal
exit

When you run this batch file, the calculation result of 7 will be displayed.

Summary

In this article, we introduced the basic usage and options of the echo command.

The echo command is a very useful command for displaying strings, environment variables, and calculation results.

Please make use of it when working with the command prompt or batch files.

#PowerShell #Command Prompt #batch files