HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to MS-DOS BatchVariablesConditions (IF)Loops (FOR)File OperationsParameters and Subroutines
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔀 Condition
🔁 Loop
⚙️ Commands & Flow
MS-DOS›Variables

Variables

Defining a variable with SET, using it with percent signs, and getting input from the user.

Simple enough for a 5-year-old to understand

Assign value with SET

Variables are defined with SET. When using its value, you wrap its name in percent signs.

Example code
1
@echo off
2
set name=Alex
Do not leave spaces around the equals sign!
VariableDo not leave spaces around the equals sign!
3
echo Hello %name%
Hello Alex author.
Output / InputHello Alex author.
VariableOutput / Input
💡 Hint: if you write set name = Alex, the space is also included in the value; do not leave a space.

Get input from the user

Example code
1
set /p city=Enter city:
Reads input from the user.
Output / InputReads input from the user.
2
echo Entered: %city%
Prints the entered value.
Output / InputPrints the entered value.
Output / Input
💡 Hint: You ask for input from the user with set /p; the entered text is written to the variable.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How is a variable's name written to use its value?

About this topic

There is only a single command for defining variables in batch files, and its syntax is very strict: leaving spaces on either side of the equals sign corrupts the variable's name or value. This is the most common mistake.

Variable values are read between percent signs. All variables are text; to perform arithmetic, a separate option must be added to the command, and that option only works with integers.

The trickiest topic is expansion time. Before running a line or a block, the command interpreter replaces the variables enclosed in percent signs with their values. Therefore, a new value of a variable modified inside a block cannot be read in the same block; delayed expansion must be enabled for this.

What you will learn

  • Variable definition
  • Space sensitivity
  • Doing arithmetic
  • Taking input from the user
  • Delayed expansion

Who is this for?

  • Those who get unexpected empty values in their scripts
  • Those trying to keep a counter inside a loop
  • Those working with environment variables

When to use?

Delayed expansion must be enabled for any variable whose value changes inside a block or loop; otherwise, the old value is read.

To prevent variable definitions made by the script from persisting in the command window, a local scope should be opened at the beginning of the script.

Common mistakes

Wrong

@echo off
set ad = Ahmet
echo Hello %ad%

The space on the left side of the equals sign becomes part of the variable name, and the space on the right goes to the beginning of the value. As a result, a weird name starting with a space is defined, and the variable attempted to be read comes out empty. No spaces should be left on either side of the equals sign.

Correct

@echo off
set ad=Ahmet
echo Hello %ad%

Wrong

@echo off
set sayac=0
for %%i in (a b c) do (
  set /a sayac=%sayac% + 1
  echo %sayac%
)
echo Total: %sayac%

Variables with percent signs inside a block are replaced with their values once before the block starts running; therefore, zero is read on every turn and the counter does not appear to advance. To read a changing value inside a block, delayed expansion must be enabled and the exclamation mark syntax must be used.

Correct

@echo off
setlocal enabledelayedexpansion
set sayac=0
for %%i in (a b c) do (
  set /a sayac=!sayac! + 1
  echo !sayac!
)
echo Total: !sayac!
endlocal

Wrong

@echo off
set /p ad=Your name:
if %ad%==Ahmet echo Hello Ahmet

If the user presses enter without typing anything, the variable remains empty and the condition line runs with a missing operand; a syntax error occurs. Also, if there are spaces in the entered value, the condition breaks. Enclosing both sides of the comparison in quotes solves both problems.

Correct

@echo off
set /p ad=Your name:
if "%ad%"=="Ahmet" echo Hello Ahmet

Frequently asked questions

What exactly does delayed expansion do?

It reads the value of the variable while the line is executing, not while the block is being parsed. To enable it, the relevant command is called at the beginning of the script and variables are written between exclamation marks instead of percent signs. It is necessary for any value that changes inside a block.

How is arithmetic done?

An arithmetic option is added to the definition command; then the right side is evaluated as an expression. It only works with integers, decimals are not supported. Division results are truncated, not rounded.

What does local scope do?

It ensures that the variables defined by the script are only valid within the script; when the script ends, the old environment is restored. If you don't enable it, variables stay in the command window and affect subsequent scripts.

Summary

No spaces are left next to the equals sign.

All variables are text.

A separate option is needed for arithmetic.

Values inside a block are expanded in advance.

Delayed expansion is read with an exclamation mark.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code