Open In App

Batch Script – String Interpolation

Last Updated : 27 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

String interpolation is a technical way of saying parsing a value that might be a variable into a string. It is an important part of dealing with the application that contains texts in some way to make them dynamic and programmatic. In batch scripting, we can perform string interpolation as well as it is technically a programming language for the command prompt (Windows OS). 

So, in this article, we shall discuss how to perform string interpolation in batch scripts.

Simple String Interpolation

To parse any value of a variable inside a string, we can use the % % operators around the variable name. We can take a simple example script, and see the syntax in it.

@echo off 

SET name=python
SET year=1991

SET output=%name% is a programming language created in %year%

echo %output%

We can see that the variable name and year which are both string variables are parsed into a string output and are echoed to the prompt. The %% operators are used to expand the literal value of the variable and thus we can interpolate the string inside of a batch script.

We can even use integers as variables in the script to interpolate them inside of the string. 

@echo off 

set name=Kevin
SET /A age=15+4

SET output=%name% is %age% years old

echo %output%

The above script has two variables, one string name, and the other an integer age, we can explicitly create a variable as an integer with /A operator before the variable name. Thus, we were able to interpolate the integer and string variables into a string, the output variable can even be an echo command to just print out the string directly without storing it into a variable.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads