The Wayback Machine - https://web.archive.org/web/20240604171126/https://www.geeksforgeeks.org/batch-script-working-with-environment-variables/
Open In App

Batch Script – Working with Environment Variables

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Environment Variables refer to the variables which are globally declared and can be accessed by the processor under the management of OS like Windows, Mac, and Linux. In starting they were meant to store the path locations but now they also work with Batch Script. The data of batch programs like input from CMD prompt, text files, log files, etc are stored under this. The user can easily change the search path using a batch file so that the applications can be run efficiently. If a variable is undefined and a batch file is scanned, some unwanted execution can occur.

An example is given below:-

@echo off
set /p result = Want to say Hello ? (Y/N) ?
if /i "%result:~,1%" EQU "Y" echo Hello Ji!
if /i "%result:~,1% EQU "N" echo Bye Bye
pause

 

  • “Y” input from the user will lead to print hello as mentioned above.
  • “N” input from the user will lead to print bye-bye. 

Benefits of Environment Variables

  • Efficient Execution Performance: It boosts the performance of the batch script so the commands which are executing run in less time and space complexities. 
  • Make script application secure: Credential information got highly secured after using environment variables
  • Reduce Execution mistakes: Runtime execution errors reduces to a large extent after developing environment variables as it configures them properly.

Environment expressions are as follows:

  • %name:~n% – It skips the first “n” letters and returns the remaining.
  • %name:~n,m% – It skips “n” letters and returns the next “m”
  • %name:,m% – Leftmost “m” number of letters.
  • %name:~-m% – Rightmost “m” number of letters.

Output Examples of the above expressions:

 

1.) ECHO  %var% :

 

 

2.) ECHO  %var:~2,3% :

 

 

3.) ECHO  %var:~-3% :

 

 

4.) ECHO  %var:~,3% :

 

 

5.) ECHO  %var:~2%:

 

 


Similar Reads

Batch Script - How to check if a process is running via a Batch Script
Given a process or application (.exe) file, the task is to create a batch script that checks whether the process is running or not and output the same. Example: In this example, we have created a batch script by the name process.bat. We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to
2 min read
Batch Script - Environment Variables
Environment variables are a collection of dynamic named values that are saved on the system and utilized by programs that are running in shells or subshells on Linux and Unix-based systems. An environment variable is, to put it simply, a variable with a name and a corresponding value. You can alter the system's operation and the actions of its prog
6 min read
Batch Script - Local VS Global Variables
In this article, we will see the differences between local and global variables in bash scripting. Variable: The name given to a memory location that is used to store values in a program is called variables. It stores information that can be called and manipulated wherever needed in the program. Scope: The part or extent of the program where the va
4 min read
Bash Script - Difference between Bash Script and Shell Script
In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capable of running a script. A script that is passed to
3 min read
Bash Script - Working of Bash Variables
A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one
7 min read
Batch Script - Return code
Return codes are the codes returned by programs when they are executed. If the command line is successful, it should return zero; if it is not successful, it should return non-zero. If the test fails, a non-zero value indicates the error number, and the user can attempt to resolve it by navigating to the error message. The test may also return an e
3 min read
Batch Script - Echo Command
Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on the console or in a text file. In batch scripting,
8 min read
Batch Script - Iterating Over an Array
A batch script is a set of instructions or a script for SOS and Windows Operating System. We can write them in the CMD line by line or we can create a file with a ".bat" or ".cmd" extension. The file can contain valid instructions for executing by the interpreter. The meaning of batch is the non-interactive execution of the instructions. We can add
3 min read
Batch Script - String Concatenation
String Concatenation is combining two or more strings to create a new string. For instance, let a string "Good" and take another string "Morning" now by string concatenation i.e. "Good" + "Morning" and we got new string i.e. "Good Morning". This is string concatenation. Let see some examples of string concatenation using batch script. Example 1 : I
2 min read
Batch Script - Remove All Spaces
In this article, we are going to see how to remove all spaces from any string using Batch String. Example : Input: G e e k s f o r G e e k s Output: GeeksforGeeksApproach :By using ' set ' we are getting input of any string.Example: set str=input stringIn the next line using ' echo %str% ' we are printing our input string.In the next line of code,
1 min read
Article Tags :