I need to run a windows command n times within a bat script file. I know how to do this in various programming languages but cannot manage to get it right on the windows command line :-(

What is the best way to emulate a do-while loop in Bash? I could check for the condition before entering the while loop, and then continue re-checking the condition in the loop, but that's duplica. ECHO Start of Loop FOR /L%i IN (1,1,5) DO ( ECHO%i ) The 1,1,5 is decoded as: (start,step,end) Also note, if you are embedding this in a batch file, you will need to use the double percent sign (%%) to prefix your variables, otherwise the command interpreter will try to evaluate the variable%i prior to running the loop.

I would expect something like either

or even this (though not entirely seriously)

Thanks!

EDIT

I can write a program in java, perl, c or whatever that will generate a bat script that looks like this

and so on. Or even 'better':

and then execute it... But the thing is that I need a concise way to specify a range of numbers to iterate through within the script.

Thanks!

raoulsson
raoulssonraoulsson
1,8986 gold badges27 silver badges29 bronze badges

3 Answers

You can do it similarly like this:

The 1,1,5 is decoded as:

Also note, if you are embedding this in a batch file, you will need to use the double percent sign (%%) to prefix your variables, otherwise the command interpreter will try to evaluate the variable %i prior to running the loop.

GoyuixGoyuix
2,6194 gold badges25 silver badges34 bronze badges
AndyAndy
ExamplesBash While Dead LoopLoop

Directly from the command line:

Using a batch file:

Displays:

jscott
22.1k6 gold badges61 silver badges90 bronze badges
fmoraesjrfmoraesjr

protected by Michael HamptonMar 28 '14 at 0:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged windowsbatchscripting or ask your own question.

On Unix-like operating systems, until and while are functions of the shell that define a block of code, and a conditional statement.

If while is used, the block loops as long as the condition is true. When the condition is false, the loop terminates.

Bash Read User Input While Loop

If until is used, the block loops as long as the condition is false. When the condition is true, the loop terminates.

This document covers the bashbuilt-in versions of until and while.

Syntax

Examples

User is repeated prompted for a name of a file to be located, until the user chooses to finish execution.

Related commands

Bash While Dead Loop Song

break — Break out of a while, for, foreach, or until loop.
csh — The C shell command interpreter.
ksh — The Korn shell command interpreter.
sh — The Bourne shell command interpreter.