2

How do I pass multiple commands to start?

I tried

start "window title" echo 1 && echo 2

But, predictably, start only gets "echo 1" and thus "1" is echoed in the new window, and "2" is echoed in the first window.

Is there some way to "escape" the && operator, or another way to pass multiple commands into start so they run successively in the new window?

2
  • Do you want both 1 and 2 to be echo-ed on the same window? Commented Oct 6, 2021 at 17:56
  • Yes, in the newly spawned window from start. Commented Oct 6, 2021 at 17:56

3 Answers 3

3

How do I pass multiple commands to start?

You need to use the cmd /k option and also quote the command you are running.

The following command will work:

start "window title" cmd /k "echo 1 && echo 2"

where:

Options   
   /C     Run Command and then terminate

   /K     Run Command and then return to the CMD prompt.
          This is useful for testing, to examine variables

   Command : The command, program or batch script to be run.
             This can even be several commands separated with '&' 
             (the whole should also be surrounded by "quotes")

Source: CMD.exe (Command Shell) - Windows CMD - SS64.com


Further Reading

5
  • Thanks, and what if one of my command parameters has quotation marks in it? Commented Oct 6, 2021 at 17:59
  • In this particular case you don't need to do anything as start "window title" cmd /k "echo "1" && echo "2"" works
    – DavidPostill
    Commented Oct 6, 2021 at 18:04
  • I've found that nested quotation marks don't always work. So escaping per @ReddyLutonadio might be a good approach. Commented Oct 6, 2021 at 20:03
  • @PatrickSzalapski And as I've commented on his answer "That puts a space before the 1"
    – DavidPostill
    Commented Oct 6, 2021 at 21:07
1

Another method is to escape the & with ^.

start "window title" echo 1 ^&^& echo 2
2
  • Hmm. That puts a space before the 1
    – DavidPostill
    Commented Oct 6, 2021 at 18:42
  • Combining both approaches works without a space: start "window title" cmd /k echo 1 ^&^& echo 2 Commented Oct 7, 2021 at 21:52
0

Just in case this might help someone with a similar issue (wanting to launch a cmd window with a title, echoing a string of commands, then executing those commands - useful for cmd prompt debug):

start "net stop w32time && net start w32time" cmd /k echo net stop w32time ^^^&^^^& net start w32time ^&^& net stop w32time ^&^& net start w32time

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .