Basics of Batch Scripting
Last Updated :
29 Sep, 2022
Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. It is not commonly used as a programming language and so it is not commonly practiced and is not trending but its control and dominance in the Windows environment can never be neglected. Almost every task and every action can be performed and executed by a simple sequence of commands typed on the Windows Command Prompt.
Batch Script execution
There are 2 ways to execute a batch script.
- Type the batch script in the command prompt.
- Write the code of script in a file and execute it through the command prompt.
Typing commands again and again on the terminal can be a very tedious task to do if we have a very lengthy code. So option 2 is generally preferred to create batch files.
Creating Batch Files
Steps to create a Batch file are pretty simple:-
- Create a new text file with a ‘.txt‘ extension.
- Now rename this file with extension as ‘.bat‘ this creates a Batch file.
- Now open this .bat file in any text editor and start scripting.
To begin scripting we must be aware of the commands of the batch interface. The commands of Batch are sometimes similar to Linux Scripting commands.
Batch Commands
Basic batch commands are all case insensitive and can be used to perform a specific set of instructions:-
- DIR – The ‘dir’ command is used to get all the directories, sub-directories, and files present in the current working directory.
- CD – The ‘cd’ command is used to change the current working directory.
- VER – The ‘ver’ command tells the version of the user’s Windows.
- CLS – The ‘cls’ command is used to clear the screen of the command prompt.
- ECHO – The ‘echo’ command is by default ‘on’ but if we turn it off by ‘echo off’ it turns off prompt till the time ‘echo on’ is passed.
- @ – The ‘@’ if used before any command hides which command is running.
- @ECHO OFF – This commands serves as the start point to any basic batch script as it hides the prompt with ‘echo off’ and hides ‘echo off’ command with ‘@’.
- HELP – This command tells us all about the commands available in the cmd. It runs only if the cmd is run as an administrator.

How to execute a batch command through cmd(command prompt)
Data Types in Batch
- Integers – Batch supports the whole set of positive and negative integers
- Strings – Unlike most programming languages we rarely use (“”) double-quotes here but we use ‘echo‘ command to print strings
Note: Batch doesn’t support floating-point values i.e. values with precision.
Variables in Batch Scripting
A variable is an entity that stores a specific value and allows the user to perform any set of instructions on it. To create variables we use the command “SET” command. A variable, unlike many programming languages, can be assigned simply without specifying any data type to it.
SET my_variable=Hello World
To print this variable we need to use the command ECHO but with a slight variation. Since echo prints both strings and variables to print string we simply write the string after ECHO as
ECHO Hello World
But to print a variable we use ECHO in a different way bypassing the variable names inside two percent signs (%) so that variable name doesn’t become a string-
ECHO %my_variable%
Working with Batch Scripts
Creating our own Batch Scripts
Example 1: To print “GeeksForGeeks” on the command prompt with and without using a variable.
Without using a variable
ECHO GeeksForGeeks
With a variable
SET my_var=GeeksForGeeks
ECHO %my_var%

Arithmetic Operators in a Batch Script
List of operators :
SET /A sum=1+1 ::addition operator
ECHO %sum%
SET /A mul=7*9 ::multiplication operator
ECHO %mul%
SET /A div=9/3 ::Division operator
ECHO %div%
SET /A assign=10 ::Assignment operator
ECHO %assign%
SET /A assign+=15 ::Increment then assignment operator
ECHO %assign%
SET /A mod= 10%3 ::Modulus/Remainder operator
ECHO %mod%

Demonstration of all arithmetic operators
Similar Reads
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
grep command in Unix/Linux
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Sed Command in Linux/Unix With Examples
The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing. With SED, you can manipulate text files wi
9 min read
25 Basic Linux Commands For Beginners [2025]
While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
Linux/Unix Tutorial
Linux is a widely-used open-source operating system, similar to Windows, Mac, and Android. It shares similarities with Unix, another operating system known for its commercial use. Unix and Linux have comparable components, including the kernel, shell, and programs. Many commands in Unix and Linux ex
12 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
ZIP command in Linux with examples
In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
7 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
7 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This articl
10 min read
File Allocation Methods
The allocation methods define how the files are stored in the disk blocks. There are three main disk space or file allocation methods. Contiguous Allocation Linked Allocation Indexed Allocation The main idea behind these methods is to provide: Efficient disk space utilization. Fast access to the fil
5 min read