Open In App

Batch Script – Echo Command

Improve
Improve
Like Article
Like
Save
Share
Report

Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on the console or in a text file.

In batch scripting, the echo command can be used to display a message, variable value, or system information on the console. The command can be followed by a message or text string enclosed in double quotes. For example, echo “Hello, World!” will display the message “Hello, World!” on the console.

The echo command can also be used to display the value of a variable. To display the value of a variable, the variable name must be preceded by a percent sign (%) and enclosed in double quotes. For example, if a variable named username contains the value “John”, the command echo “Welcome, %username%” will display the message “Welcome, John” on the console.

Additionally, the echo command can be used to redirect the output to a file, rather than displaying the message on the console. This can be done by using the > operator followed by the name of the file. For example, echo “Hello, World!” > output.txt will create a file named “output.txt” and write the message “Hello, World!” to the file.

In most of the modern and traditional operating systems, there are one or more user interfaces (e.g. Command Line Interface(CLI), Graphical User Interface(GUI), Touch Screen Interface, etc.) provided by the shell to interact with the kernel. Command Prompt, PowerShell in Windows, Terminal in Linux, Terminology in Bodhi Linux, and various types of Terminal Emulators [also called Pseudo Terminals] (e.g. Cmder, XTerm, Termux, Cool Retro Term, Tilix, PuTTY, etc.) are the examples of CLI applications. They act as interpreters for the various types of commands we write. We can perform most of the required operations (e.g. I/O, file management, network management, etc.) by executing proper commands in the command line.

If we want to execute a series of commands/instructions we can do that by writing those commands line by line in a text file and giving it a special extension (e.g. .bat or .cmd for Windows/DOS and .sh for Linux) and then executing this file in the CLI application. Now all the commands will be executed (interpreted) serially in a sequence (one by one) by the shell just like any interpreted programming language. This type of scripting is called Batch Scripting (in Windows) and Bash Scripting (in Linux). 

Why use Batch Script – Echo Command?

Here are a few reasons why the echo command is commonly used:

  1. Displaying messages: The echo command can be used to display messages or information on the console or in a text file. This is useful for providing feedback to the user, displaying error messages, or providing instructions.
  2. Displaying variables: Batch scripts often use variables to store information or data. The echo command can be used to display the value of a variable on the console or in a text file, making it easier to debug and troubleshoot scripts.
  3. Debugging: The echo command can be used to debug scripts by displaying the values of variables, commands, or system information. This can help identify errors and improve the efficiency of scripts.
  4. File output: The echo command can be used to redirect output to a file, making it easier to save and share information. This can be particularly useful when generating reports or logs.
  5. Script automation: Batch scripts can automate repetitive tasks, making them more efficient and less prone to human error. The echo command can be used to provide feedback and ensure that scripts are running as expected.

Advantages:

There are several advantages of using the echo command in batch scripting:

  1. Ease of use: The echo command is simple and easy to use, requiring minimal knowledge of scripting or programming. It can be used to display messages, variables, and system information quickly and easily.
  2. Debugging: The echo command can be used to debug scripts by displaying the values of variables, commands, or system information. This can help identify errors and improve the efficiency of scripts.
  3. Automation: The echo command can be used in conjunction with other batch commands to automate repetitive tasks. This can save time and reduce the likelihood of human error.
  4. Output redirection: The echo command can be used to redirect output to a file, making it easier to save and share information. This can be particularly useful when generating reports or logs.
  5. Customization: The echo command can be customized to display messages or information in different colors or formats, making it easier to distinguish between different types of information.

Disadvantages:

There are a few disadvantages of using the echo command in batch scripting:

  1. Limited functionality: The echo command is limited in its functionality and can only be used to display messages, variables, and system information. For more complex operations, additional batch commands or scripting languages may be required.
  2. Formatting limitations: The echo command has limitations when it comes to formatting messages or information. It may not be possible to customize the formatting of text or add images or graphics to messages.
  3. Compatibility issues: The echo command may not be compatible with all versions of Windows or other operating systems. This can cause issues when sharing scripts or running scripts on different machines.
  4. Security concerns: The echo command can be used to display sensitive information, such as passwords or usernames. This information may be visible in the command history or log files, making it a security risk.

Example:

Step 1: Open your preferred directory using the file explorer and click on View. Then go to the Show/hide section and make sure that the “File name extensions” are ticked.

Step 2: Now create a text file and give it a name (e.g. 123.bat) and edit it with notepad and write the following commands and save it.

echo on
echo "Great day ahead"
ver

Step 3: Now save the file and execute this in the CLI app (basically in CMD). The output will be like the following.

Explanation:

It was a very basic example of batch scripting. Hereby using echo on we ensure that command echoing is on i.e. all the commands will be displayed including this command itself. The next command prints a string “Great day ahead” on the screen and the ver command displays the version of the currently running OS. Note that the commands are not case sensitive (e.g. echo and ECHO will give the same output). Now I will discuss everything about the ECHO command.

ECHO Command: The ECHO command is used to print some text (string) on the screen or to turn the on/off command echoing on the screen.

 Syntax:

echo [<any text message to print on the screen>]

or

echo [<on> | <off>]

Using the ECHO command without any parameter:

When echo is used without any parameter it will show the current command echoing setting (on/off).

Syntax:

echo

Example:

Printing a message on the screen using ECHO:

We can print any text message on the screen using echo. The message is not needed to be enclosed within single quotes or double quotes ( ‘  or  ), moreover any type of quote will also be printed on the screen.

Syntax:

echo <any text message to print on the screen>

  Example:

Command Echoing:

  • By using echo on we can turn on command echoing i.e. all the commands in a batch file will also be printed on the screen as well as their outputs.
  • By using echo off we can turn off command echoing i.e. no commands in the batch file will be printed on the screen but only their outputs, but the command echo off itself will be printed.

Syntax:

echo [<on> | <off>]

Example:

This is an example where the command echoing is turned on.

Let’s see the output.

Example:

This is an example where the command echoing is turned off.

Let’s see the output.

Using <@echo off>:

We have seen that when we use echo off it will turn off command echoing but it will print the command echo off itself. To handle this situation we can use @echo off as it will turn off command echoing and also will not print this command itself.

Syntax:

@echo off

Example:

Let’s see the output.

Printing the value of a variable:

We can declare a variable and set its value using the following syntax.

Syntax:

set variable_name=value

We can print the value of a variable using the following syntax.

Syntax:

echo %variable_name%

Note that we can put the %variable_name% anywhere between any text to be printed.

Example:

Concatenation of Strings:

We can concatenate two string variables and print the new string using echo.

Example:



Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads