Variables in Batch Scripting Language
Variables in Batch Scripting Language
MENU
1- What is a variable?
2- Common variables
3- Argument variables
4- Scope of variable
5- The environment variables of Windows
1- What is a variable?
Variable is a basic concept in computer science. It is a value that can change. Like other languages, Batch also has the concept of
variable.
2- Common variables
Batch language does not have a clear concept of data type. Normally, a variable has the value that is a string, including
characters after the = sign to the end of the line. (Including whilte spaces if applicable).
In case you want to declare a variable which is a number, you need to use the set /A statement
Example:
https://o7planning.org/en/11591/variables-in-batch-scripting-language 1/10
6/23/2020 Variables in Batch Scripting Language
variableExample1.bat
?
1 @echo off
2
3 set name=Tran
4 set /A age= 37
5
6 echo %name%
7 echo %age%
8
9 set info=My Name is: %name%, Age: %age%
10
11 echo %info%
12
13 pause
variableExample2.bat
?
1 @echo off
2
3 set /A a = 100
4
5 set /A b = 200
6
7 echo a = %a%
8 echo b = %b%
9
10 set /A sum = a + b
11
12 echo Sum = %sum%
13
14 pause
3- Argument variables
When calling to execute a batch file, you can pass values to the file. You can access these values through the variables such as
%1, %2, %3,... in the file. These variables are also called argument variables.
argumentVariables.bat
https://o7planning.org/en/11591/variables-in-batch-scripting-language 2/10
6/23/2020 Variables in Batch Scripting Language
?
1 @echo off
2
3 @echo First argument is: %1
4 @echo Second argument is: %2
5
6 pause
7
We test the above example by opening the CMD window and CD to go to the folder containing the argumentVariables.bat file
Results:
4- Scope of variable
https://o7planning.org/en/11591/variables-in-batch-scripting-language 3/10
6/23/2020 Variables in Batch Scripting Language
When a Command Window is opened, it will start a session. Such session will finish upon closing the Command Window. During
a session you can execute one or more batch files. The variables created in the previous file can be visited in the following file.
Such variables are called global variables. However, you can create a local variable. The local variable only exists within the file of
defining it or in a section of that file.
Local variables are declared in a block, starting with setlocal and ending with endlocal.
localVariable.bat
?
1 @echo off
2
3 set global1=I am a global variable 1
4
5 setlocal
6
7 set local1=I am a local variable 1
8 set local2=I am a local variable 2
9
10 echo ----- In Local Block! -----
11 echo local1= %local1%
12 echo local2= %local2%
13
14 endlocal
15
16 echo ..
17 echo ----- Out of Local Block! -----
18
19 echo global1= %global1%
20 echo local1= %local1%
21 echo local2= %local2%
22
23 pause
Variables, declared in the Batch file, and not located in the setlocal .. endlocal block, will be global variables. They can be used in
other files in the same session.
In this example, we have two files such as batchFile1.bat and batchFile2.bat. The MY_ENVIRONMENT variable is defined in file 1,
and which is used in file 2.
batchFile1.bat
?
1 @echo off
2
3 set MY_ENVIRONMENT=C:/Programs/Abc;C:/Test/Abc
https://o7planning.org/en/11591/variables-in-batch-scripting-language 4/10
6/23/2020 Variables in Batch Scripting Language
batchFile2.bat
?
1 @echo off
2
3 echo In batchFile2.bat
4
5 echo MY_ENVIRONMENT=%MY_ENVIRONMENT%
Open CMD, and CD to the folder containing the batchFile1.bat, batchFile2.bat files and run these files respectively.
https://o7planning.org/en/11591/variables-in-batch-scripting-language 5/10
6/23/2020 Variables in Batch Scripting Language
https://o7planning.org/en/11591/variables-in-batch-scripting-language 6/10
6/23/2020 Variables in Batch Scripting Language
Here, you can create an environment variable for current user, or a system environment variable that can be used by any user.
For example, I create a environment variable with the name of MY_ENVIRONMENT_VARIABLE and its value of "My Environment
Variable Value!".
https://o7planning.org/en/11591/variables-in-batch-scripting-language 7/10
6/23/2020 Variables in Batch Scripting Language
And you can use the environment variable created in a batch file.
testEnvVariable.bat
?
1 @echo off
2
3 echo %MY_ENVIRONMENT_VARIABLE%
4
5 pause
https://o7planning.org/en/11591/variables-in-batch-scripting-language 8/10
6/23/2020 Variables in Batch Scripting Language
Newest Documents
Android CharacterPickerDialog
Android Dialog
Android TimePickerDialog
Create a simple File Chooser in Android
Create a simple File Dialog Finder in Android
Android Wifi Scanning
Upgrade Mac Operating System
How do I take a MacOS Retina screenshot and get the image at its actual size?
iOS Tutorial for Beginners - Hello iOS
Android Phone Call
o7planning.org
https://o7planning.org/en/11591/variables-in-batch-scripting-language 9/10
6/23/2020 Variables in Batch Scripting Language
https://o7planning.org/en/11591/variables-in-batch-scripting-language 10/10