Batch Script - Echo Command - GeeksforGeeks

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). 
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.
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 is used to print some text (string) on the screen or to turn the on/off command echoing on the screen.
echo [<any text message to print on the screen>]
or
echo [<on> | <off>]
When echo is used without any parameter it will show the current command echoing setting (on/off).
echo
Example:
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.
echo <any text message to print on the screen>
  Example:
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.
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.
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.
@echo off
Example:
Let’s see the output.
We can declare a variable and set its value using the following syntax.
set variable_name=value
We can print the value of a variable using the following syntax.
echo %variable_name%
Note that we can put the %variable_name% anywhere between any text to be printed.
Example:
We can concatenate two string variables and print the new string using echo.
Example:
Batch-script
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
scp command in Linux with Examples
nohup Command in Linux with Examples
mv command in Linux with examples
Thread functions in C/C++
Docker - COPY Instruction
chown command in Linux with Examples
nslookup command in Linux with Examples
SED command in Linux | Set 2
Named Pipe or FIFO with example C program
uniq Command in LINUX with examples