Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

windows - 如何在Windows CMD的一行中运行两个命令?(How do I run two commands in one line in Windows CMD?)

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上如何完成?)

  ask by flybywire translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(自2000年以来,在所有Microsoft操作系统上都采用这种格式,并且在今天仍然不错:)

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.

(在一行上执行多个命令的单一&语法可以追溯到Windows XP,Windows 2000和某些早期的NT版本。)

(4.0 at least, according to one commenter here.)

((根据一位评论者的说法,至少为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.

(在此之前,&&语法只是外壳替换4DOS的一项功能,然后将该功能添加到Microsoft命令解释器中。)

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版本的命令解释器,(未记录)命令分隔符为字符20(Ctrl + T),在此将用^ T表示。)

dir ^T echo foo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...