我想在Windows CMD控制台中运行两个命令。

在Linux中我会这样做

touch thisfile ; ls -lstrh

在Windows上是怎么做的呢?


当前回答

&是Bash中等价的;&&是Bash中的&&(只在前一个没有导致错误时才运行命令)。

其他回答

嗯,你有两个选择:管道,或只是&:

DIR /S & START FILE.TXT

Or,

tasklist | find "notepad.exe"

管道(|)主要用于获取一个命令的输出,并将其输入到另一个命令中。(&)表示运行这个和那个。

文档中的一段话:

来源:微软,Windows XP专业产品文档,命令shell概述 另外:Windows CMD命令的A-Z索引

Using multiple commands and conditional processing symbols You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol. For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful. You can use the special characters listed in the following table to pass multiple commands. & [...] command1 & command2 Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command. && [...] command1 && command2 Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully. || [...] command1 || command2 Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero). ( ) [...] (command1 & command2) Use to group or nest multiple commands. ; or , command1 parameter1;parameter2 Use to separate command parameters.

自2000年以来,所有的微软操作系统都是这样的,直到今天仍然很好:

dir & echo foo

如果你希望第二个命令只在第一个命令成功退出时执行:

dir && echo foo

在一行上执行多个命令的单个&(&)语法可以追溯到Windows XP、Windows 2000和一些较早的NT版本。(根据这里的一个评论,至少4.0。)

关于这一点,您还可以在滚动本页面时找到其他一些观点。

下面是一些历史数据,供那些觉得有教育意义的人参考。

在此之前,&&语法只是shell替代品4DOS的一个特性,之后该特性被添加到微软命令解释器中。

在Windows 95、98和ME中,你可以使用管道字符:

dir | echo foo

在MS-DOS 5.0及以后版本中,通过一些较早的Windows和NT版本的命令解释器,(未记录的)命令分隔符是字符20 (Ctrl+T),这里我将用^T表示。

dir ^T echo foo

在windows中使用&符号可在一行中使用命令

C:\Users\Arshdeep Singh>cd Desktop\PROJECTS\PYTHON\programiz & jupyter notebook

比如Linux 我们使用,

touch thisfile ; ls -lstrh

为了同时执行两个命令,必须在两个命令之间加上&(&)符号。像这样:

color 0a & start chrome.exe

干杯!