Open In App

Batch Script – Working with Environment Variables

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

 

Benefits of Environment Variables

Environment expressions are as follows:

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%:

 

 

Article Tags :