Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Batch Script – Echo Command

  • Last Updated : 05 Nov, 2021

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). 

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.

The 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:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!