How do I run two commands in one line in Windows CMD?

后端 未结 19 1107
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:45

I want to run two commands in a Windows CMD console.

In Linux I would do it like this

touch thisfile ; ls -lstrh

How is it done on

19条回答
  •  天涯浪人
    2020-11-22 01:08

    Like this on all Microsoft OSes since 2000, and still good today:

    dir & echo foo
    

    If you want the second command to execute only if the first exited successfully:

    dir && echo foo
    

    The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

    There are quite a few other points about this that you'll find scrolling down this page.

    Historical data follows, for those who may find it educational.

    Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

    In Windows 95, 98 and ME, you'd use the pipe character instead:

    dir | echo foo
    

    In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

    dir ^T echo foo
    

提交回复
热议问题