3

This question is based off @ZaLiTHkA's reply of the accepted answer of the running cmd commands sequentially question seen below.

Just for the sake of clarity: you should note the single & symbol here is explicitly for a live command prompt. When commands are run by passing a string to be executed as a script (shortcuts, batch files, etc.. ), then double && are required – ZaLiTHkA Apr 3 '14 at 20:28

Is && really necessary for running script commands (i.e. shortcuts or batch files)? I've found I am still able to run a simple command like: cmd /k echo foo & echo bar in the run window and execute a shortcut of the same target/command, without a problem. Would appreciate any clarification or documentation explicitly saying why and if && is actually the recommended method for batch scripts since it seems a single & seems to work as well.

Note: This is a not a question distinguishing the difference between && and & definitions, but why && is required as pointed out by @ZaLiTHkA's very visibly upvoted comment.

decoder247
  • 134
  • 2
  • 12
  • My advice is to always ignore points/votes, just because something has been voted for by people doesn't make it correct, which in this case is true. – Compo Feb 21 '20 at 14:57
  • 1
    ss64 is very useful, see [How-to: Conditional Execution](https://ss64.com/nt/syntax-conditional.html) – ScriptKidd Feb 22 '20 at 01:14

1 Answers1

5

Just for the sake of clarity: you should note the single & symbol here is explicitly for a live command prompt. When commands are run by passing a string to be executed as a script (shortcuts, batch files, etc.. ), then double && are required. – ZaLiTHkA Apr 3 '14 at 20:28

The comment is nonsense!

As the answer already explained, there is a semantical difference between & and &&.

It only depends on the intention, which one should be used.

In the case of echo a & echo b, there is no good reason to ever use && instead, because the second echo shall be executed, independent of the first echo.

In the case of cd C:\myDir\ && del *.* it's wise to use && to prevent unwanted deletion somewhere, when the cd fails.

I comment on that, too.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 2
    In other words, when using commandX && commandY the commandY is only executed if commandX succeeds; – AlexD Dec 21 '20 at 23:40