The Wayback Machine - https://web.archive.org/web/20240717024533/https://www.geeksforgeeks.org/batch-script-string-interpolation/
Open In App

Batch Script – String Interpolation

Last Updated : 27 Jan, 2022
Improve
Suggest changes
Post a comment
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.



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
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
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 - Replace a String
In this article, we are going to Replace a substring with any given string. Batch Script :@echo off set str=GFG is the platform for geeks. echo %str% set str=%str:the=best% echo %str% pause In the above example, we are going to replace 'the' by substring 'best' using %str:the=best% statement. Explanation : By using ' set ' we are getting input of a
1 min read
Batch Script - Empty String
In this article, we are going to create an Empty String using Batch Script. Batch Script :@echo off set str1= set str2=Hello ::using if statement we will check that str1 is empty string or not. if [%str1%]==[] echo "str1 is an empty string" if [%str2%]==[] echo "str2 is an empty string" pauseExplanation :By using ' set ' we are getting our input st
1 min read
Batch Script - Create String
In this article, we are going to learn how to create a String using Batch Script. Batch Script :@echo off set str=Geeks for Geeks echo %str% pauseBy using ' set ' we are getting input of any string.Ex: set str=input stringIn the next line, we are using ' echo %str% ' for printing our input string.Then we are using 'pause' , to hold the screen until
1 min read
Batch Script - Left String
In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content. Example 1: Batch script set str=GeeksForGeeks echo.%str% set str=%str:~0,5% echo.%str% Output GeeksForGe
1 min read
Batch Script - String length
In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop (endlocal &
2 min read
Batch Script - Mid String
In this article , we are going to learn how to use the concept of Mid String using Batch Script. Using the concept of 'Mid String' we are extracting out a sub string between two indices of any given string. Batch Script :@echo off set str=GeeksforGeeks echo %str% set str=%str:~5,-5% echo %str% pause Using above code we are going to print a substrin
2 min read
Batch Script - Right String
In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main string. Using the right string we can print characters
2 min read
Article Tags :
three90RightbarBannerImg