Stackoverflow热门问题(二十)-怎么在Windows Cmd里面一行运行多条命令?
作者:互联网
文章首发及后续更新:https://mwhls.top/2824.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。
目录 1. 怎么在Windows Cmd里面一行运行多条命令? 2. How do I run two commands in one line in Windows CMD?如有翻译问题欢迎评论指出,谢谢。
这篇文章的前三个回答都是11年写的,所以我测试了一下,第二篇回答的&, &&, ||依然能用。
怎么在Windows Cmd里面一行运行多条命令?
- flybywire asked:
- 我想在Windows的CMD控制台里面运行两个命令。
- 在LInux,我是这样写的
touch thisfile ; ls -lstrh
- 怎么能在Windows做到这个?
- Answers:
- djdanlib – vote: 1560
- Microsoft从2000年到现在都能用这个命令:
dir & echo foo
- 如果你希望第一个命令成功后才执行第二个命令:
dir && echo foo
- 单&在一个命令行中执行多条命令的能力,可以追溯到Windows XP,Windows 2000,以及更早的NT版本(至少4.0,评论里提到)
- 往下面翻翻,还有其他的几点。
- 下面是关于命令的一些历史。
- 在Microsoft添加
&&
命令前,&&
只是4DOS代替shell的一个命令。 - 在Windows 95, 98, ME,使用管道符号代替:
dir | echo foo
- 在MS-DOS 5.0以及之后的版本,更早的的Windows与NT版本的命令解释器,命令用
CTRL + T
来分割,下面用^T
表示 dir ^T echo foo
- Raihan – vote: 622
- 从文档中的引用:
来自:Microsoft, Windows XP Professional Product Documentation, Command shell overviewAlso: An A-Z Index of Windows CMD commands- 使用多命令以及条件处理符号
- 你能在一个命令行或者脚本中通过条件处理符号来执行多条命令。当你用条件处理符号执行多条命令时,符号左边命令的结果会影响符号右边命令的执行。
- 例如,你可能希望后面的命令只在前面的命令失败或者成功的时候才执行。
- 使用下面列出的特殊符号来执行多命令。
& […]
command1 & command2
用来在一个命令行中分割两条命令。Cmd.exe会先执行第一条命令,然后再执行第二条。&& […]
command1 && command2
前一个命令成功,后一条命令才能执行。|| […]
command1 || command2
前一个命令失败,后一条命令才能执行(推荐返回错误代码,而不是0)。( ) […]
(command1 & command2)
Use to group or nest multiple commands.
使用一组或嵌套命令。; or ,
command1 parameter1;parameter2
用于分割参数。
- 从文档中的引用:
- manojlds – vote: 79
&
是Bash;
的等效,&&
则是Bash&&
的等效(第二条命令仅在第一条命令没有引起错误才执行)。
- djdanlib – vote: 1560
How do I run two commands in one line in Windows CMD?
- flybywire asked:
- I want to run two commands in a Windows CMD console.
我想在Windows的CMD控制台里面运行两个命令。 - In Linux I would do it like this
在LInux,我是这样写的 touch thisfile ; ls -lstrh
- How is it done on Windows?
怎么能在Windows做到这个?
- I want to run two commands in a Windows CMD console.
- Answers:
- djdanlib – vote: 1560
- Like this on all Microsoft OSes since 2000, and still good today:
Microsoft从2000年到现在都能用这个命令: 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.)
单&在一个命令行中执行多条命令的能力,可以追溯到Windows XP,Windows 2000,以及更早的NT版本(至少4.0,评论里提到) - 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.
在Microsoft添加&&
命令前,&&
只是4DOS代替shell的一个命令。 - In Windows 95, 98 and ME, you\’d use the pipe character instead:
在Windows 95, 98, ME,使用管道符号代替: 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.
在MS-DOS 5.0以及之后的版本,更早的的Windows与NT版本的命令解释器,命令用CTRL + T
来分割,下面用^T
表示 dir ^T echo foo
- Like this on all Microsoft OSes since 2000, and still good today:
- Raihan – vote: 622
- A quote from the documentation:
Source: Microsoft, Windows XP Professional Product Documentation, Command shell overviewAlso: An A-Z Index of Windows CMD commands
从文档中的引用:
来自:Microsoft, Windows XP Professional Product Documentation, Command shell overviewAlso: An A-Z Index of Windows CMD commands- 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.
用来在一个命令行中分割两条命令。Cmd.exe会先执行第一条命令,然后再执行第二条。&& […]
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).
前一个命令失败,后一条命令才能执行(推荐返回错误代码,而不是0)。( ) […]
(command1 & command2)
Use to group or nest multiple commands.
使用一组或嵌套命令。; or ,
command1 parameter1;parameter2
Use to separate command parameters.
用于分割参数。
- Using multiple commands and conditional processing symbols
- A quote from the documentation:
- manojlds – vote: 79
&
is the Bash equivalent for;
( run commands) and&&
is the Bash equivalent of&&
(run commands only when the previous has not caused an error).&
是Bash;
的等效,&&
则是Bash&&
的等效(第二条命令仅在第一条命令没有引起错误才执行)。
- djdanlib – vote: 1560
标签:commands,run,Windows,Cmd,命令,command,&&,Stackoverflow 来源: https://blog.csdn.net/asd123pwj/article/details/119476541