I know how to do this in /bin/sh, but I'm struggling a bit in Windows.

I know you can do arithmetic on 32-bit signed integers with

SET /a 2+2

But how do I pass this to the next command? For example, the process I want to perform is as follows.

Consumer editions of Windows have no native automated sleep function (I believe?) -- the best way to perform a sleep is to use PING in association with the -n switch to get that many seconds, minus one, of sleep. The following command is effective for a silent sleep:

PING localhost -n 3 > NUL

But I want to alias this into a sleep command. I'd like to have it elegant so that you enter the actual number of seconds you want to sleep after the command, right now I can do


Which works, but it's always 1 second less than your input, so if you wanted to sleep for one second you would have to use the command SLEEP 2. That's not exactly ideal.

Is there some way for me to pass the arithmetic of $1+1 and pass it on to the next command in Windows? I assume there is some way of using STDOUT...


Give this a try:

DOSKEY SLEEP==set /a t=$1+1 $G NULL $T PING -n %t% $G NULL

The $T is a command separator equivalent to &.

Note that the > NUL redirects the output of DOSKEY rather than the macro. You can see that it's not included if you do DOSKEY /MACROS. To do the redirection that you want, append $G NUL to the end of the line as shown above.

Something like this will also work:

DOSKEY SLEEP=for /f %t in ('set /a $1+1') do @PING -n %t $G NUL

Try both and see which one works better for you.

  • That's absolutely perfect. Wow, I didn't realise the Windows command line would be this bad. :D Dec 23 '10 at 21:09
  • beautiful i must say
    – bubu
    Dec 24 '10 at 1:50
  • +1. This is why real hackers love .bat files: There's nothing more hacky!
    – phogg
    Dec 27 '10 at 19:17
  1. As far as I know cmd.exe does not offer readily available function for in-execution evaluation of function.

  2. If you just wanted to wait for 1 more second, just insert another ping -n 2 into it.

  3. You can consider installation of sleep command from the windows server administration tools.

  4. How about an installation of cygwin/powershell?

EDIT: correct my ping parameter stupidity

  • Not possible, this needs to be deployed to clients without cygwin/powershell and needs to be standalone as a batch file. Another ping -n 1 as it executes and terminates nearly instantaneously. Dec 23 '10 at 16:37
  • should have been another "ping -n 2" sorry my gross stupidity. should have taken the clue that ping -n [k] will wait for k-1 seconds...
    – bubu
    Dec 23 '10 at 16:38
  • Hm, I suppose that's a possibility, but not really an elegant one (not that this is going to be elegant anyway... Windows command line never is). Dec 23 '10 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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