Calculating the sum of two variables in a batch script
NickName:swarajd Ask DateTime:2012-05-21T00:00:27

Calculating the sum of two variables in a batch script

This is my first time on Stack Overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine.

Here is the code I have been using:

@echo off
set a=3
set b=4
set c=%a%+%b%
echo %c%
set d=%c%+1
echo %d%

The output of that is:

3+4
3+4+1

How would I add the two variables instead of echoing that string?

Copyright Notice:Content Author:「swarajd」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10674974/calculating-the-sum-of-two-variables-in-a-batch-script

Answers
staticbeast 2012-05-20T16:41:55

You need to use the property /a on the set command.\n\nFor example, \n\nset /a \"c=%a%+%b%\"\n\n\nThis allows you to use arithmetic expressions in the set command, rather than simple concatenation.\n\nYour code would then be:\n\n@set a=3\n@set b=4\n@set /a \"c=%a%+%b%\"\necho %c%\n@set /a \"d=%c%+1\"\necho %d%\n\n\nand would output:\n\n7\n8\n",


lolzeryest 2015-02-09T01:48:09

According to this helpful list of operators [an operator can be thought of as a mathematical expression] found here, you can tell the batch compiler that you are manipulating variables instead of fixed numbers by using the += operator instead of the + operator.\n\nHope I Helped!",


Richie 2016-10-25T04:13:20

You can solve any equation including adding with this code:\n\n@echo off\n\ntitle Richie's Calculator 3.0\n\n:main\n\necho Welcome to Richie's Calculator 3.0\n\necho Press any key to begin calculating...\n\npause>nul\n\necho Enter An Equation\n\necho Example: 1+1\n\nset /p \n\nset /a sum=%equation%\n\necho.\n\necho The Answer Is:\n\necho %sum%\n\necho.\n\necho Press any key to return to the main menu\n\npause>nul\n\ncls\n\ngoto main\n",


Luke 2016-05-17T03:53:18

@ECHO OFF\nTITLE Addition\nECHO Type the first number you wish to add:\nSET /P Num1Add=\nECHO Type the second number you want to add to the first number:\nSET /P Num2Add=\nECHO.\nSET /A Ans=%Num1Add%+%Num2Add%\nECHO The result is: %Ans%\nECHO.\nECHO Press any key to exit.\nPAUSE>NUL\n",


Tamil selvan 2014-08-07T11:29:09

@ECHO OFF\nECHO Welcome to my calculator!\nECHO What is the number you want to insert to find the sum?\nSET /P Num1=\nECHO What is the second number? \nSET /P Num2=\nSET /A Ans=%Num1%+%Num2%\nECHO The sum is: %Ans%\nPAUSE>NUL\n",


More about “Calculating the sum of two variables in a batch script” related questions

Calculating the sum of two variables in a batch/UNIX single piped line

I would like to execute some basic mathematical calculations in batch/Cygwin, but the solution, described in StackOverflow question: Calculating the sum of two variables in a batch script is using ...

Show Detail

Calculating the sum of two variables in a batch script

This is my first time on Stack Overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine. Here is th...

Show Detail

Recursive Batch script for calculating pow

I'm crashing my head tring to understand where the error is in this Batch script, used for calculating the pow of a given number through recursion of the function "pow", as many times as the order ...

Show Detail

Pytorch: correct way to sum batch loss with epoch loss

I'm calculating two losses. One per batch and one per epoch, at the end of the batches loop. When I try to sum these two losses I get the following error: RuntimeError: one of the variables needed ...

Show Detail

calculating sum of two triangular random variables (Matlab)

I would like to calculate the sum of two triangular random variables, P(x1+x2 < y) Is there a faster way to implement the sum of two triangular random variables in Matlab? EDIT: It seems t...

Show Detail

Script calling batch file that sets environment variables

I need to write a script, on Windows, that calls a batch file to set some environment variables, then carries out some actions that make use of those environment variables. If the calling script is

Show Detail

Use Bamboo variables in batch script

According to this very old question you can use Bamboo variables in a batch script like %bamboo_buildNumber%, but it doesn't work for me, I just get an empty string. I also tried %bamboo.buildNumber%

Show Detail

batch script conditions on variables

I want to use the first argument of my batch script as a variable. If it contains FULL I will set all my variables to 1 and if it's empty I will ask for each. But I can't make my code work : @ec...

Show Detail

Set environment variables by batch file called from perl script

Let's consider the following perl script: #!/usr/bin/perl system("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/Tools/VsDevCmd.bat"); system("msbuild"); The batch file in...

Show Detail

How to run two for loops in parallel in batch script

As a part of an assignment, I need to run two for loops in parallel in batch script. How can I run two for loops in parallel in batch script. In brief, I am using the following code to replace the

Show Detail