Batch Scripting

Miscellaneous tips to build useful one-liners or bash scripts.

Table of Contents


Warning

In batch script files, reference variables with two %% instead of one % or this will fail. This is not the case for one-liners. This is because you need to escape the % char with the escape char which is also %… Thanks Microsoft.


File Descriptors

Name Description
nul Equivalent to /dev/null
1 (or nothing) Standard Output
2 Standard Error

FOR loops

Loop over a simple counter:

for /L %i in ([start],[step],[stop]) do [command]

Infinite loop:

for /L %i in (1,0,2) do [command]

Loop over lines of a file:

for /F %i in ([filename]) do [command]

Loop over the output of a command:

for /F %i in ('[command]') do [command]

Loop over a string:

for /F %i in ("[string]") do [command]

Pause Execution

timeout /t [seconds] /nobreak

Run Multiple Commands

Use & and && to run multiple commands on a single line: