為什麼在MS-DOS的批處理腳本中將"回顯"選為默認設置?


32

根據我的經驗,幾乎每個單個MS-DOS(以及Windows cmd)批處理文件都以@echo off行開頭,以靜默關閉批處理文件中向控制台的命令回顯。這似乎不必要地嘈雜,並引發了標題問題:為什麼批處理腳本中的echo默認不為off

我進行了一些搜索,但是沒有發現任何直接說明這一點的內容。看來this answer可能觸及了導致該決定的某些因素,但據我所讀,並未直接回答此問題。

我能想到的一件事是Microsoft docs的以下剪裁的結果:

  • After echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt, type echo on.

我可以看到,早期的MS-DOS解釋器沒有確定他們正在執行的命令範圍的任何特定方式;換句話說,他們無法分辨是運行直接鍵入的控制台命令還是來自批處理文件的命令,因此echo設置是有效的(或實際上)是全局的。因此,由於大多數時間(?)用戶希望在控制台上顯示提示,因此echo on成為明智的默認值...。從那以後,也許微軟只是繼承了echo on的默認值保持向後兼容性?我不知道這是否與它有任何關係。

14

The choice for the default is between seeing what's going on and not seeing what's going on.

Specifically, should each command line of a batch file be shown as it's executed or not.

It seems reasonable to show them to assist in debugging, rather than hide them until you know the command in the manual that turns it on. This was in the days of reading the manual, if you had one, or asking someone who knew, if you had them to ask. No internet for further information.

In modern systems, the default is usually a 'verbose' tell-me-more mode for prompts as suchlike, with an option sometimes to select reduced/no prompts for experienced users.


6

In most cases, having commands echo to the console was harmless, and helped provide a form of progress indicator for whatever task the batch file was performing. The only times that such echo would be problematic would be when the batch file was trying to format a screen layout for itself. Having a screen look like:

A>echo Please type one of the following commands:
Please type one of the following commands:

A>echo WOOZLE -- Run the Woozle program
WOOZLE -- Run the Woozle program

A>echo FNORBLE -- Do an inventory of the Fnorbles on your system
FNORBLE -- Do an inventory of the Fnorbles on your system

etc.

Although a batch file could include messages within a batch file preceded by REM and output them with echo on, some people might have their prompt set to something that's rather long, or even has multiple lines, and trying to predict how to format things meaningfully could be difficult.

In short, having echo on by default wasn't objectionable. In fact, until the introduction of DOS 3.0 it wasn't possible to suppress the display of the first command in a batch file, so the execution of batch files that were supposed to display messages would often start with something like A>ECHO OFF, but that wasn't a particular problem.

Note that while there are some kinds of batch files that routinely start with @echo off, there were many users who ran their own batch files much more often than those from externally-produced software, and would seldom have reason to disable echo.


41

ECHO ON was chosen as the default setting when interpreting batch files to preserve backwards compatibility. In PC-DOS 1.0, COMMAND.COM displayed each command as it interpreted it, and this couldn’t be disabled. ECHO was added in PC/MS-DOS 2.0, with a dual purpose (displaying messages, and controlling the display of batch file commands); its default is ON so that the behaviour of batch files written for DOS 1.0 doesn’t change.

As for why DOS 1.0 displayed each command as it interpreted it, a number of reasons spring to mind:

  • as john_e pointed out, CP/M 2.2 behaved in the same way, so this could be “backwards compatibility” taken a step further (or familiarity with CP/M, or any other shell with similar behaviour, driving design decisions);
  • the behaviour remains similar to that seen when entering commands one at a time — users see the prompt, the command, and whatever it outputs;
  • when using parameters, the user would see exactly how the parameters were applied, which would help understand this potentially unfamiliar new feature.

See page 3-18 of the PC-DOS 1.0 manual for an example of the latter: the manual describes a batch file, ASMFILE.BAT containing

Copy %1.MAC %2.MAC
Type %2.PRN
Type %0.BAT

and the user would then see the substitutions, as illustrated:

ASMFILE A:PROG1 B:PROG2

would show

A>Copy A:PROG1.MAC B:PROG2.MAC
A>Type B:PROG2.PRN
A>Type ASMFILE.BAT

and their results.

@ was added in version 3.3.


15

Great stuff on the the MS/PC-DOS and CP/M history in the other answers.

I'd add that batch files come from the heritage (in early DOS, CP/M, and on mainframes and other systems) of script files, intended to save the "console operator" typing and allow them to leave the "console" while the potentially time-consuming steps were being sequentially executed. The on-screen (CRT) or printed log of commands and input entered, and output generated would document what was done, and there would be no reason to hide the command input, whether typed manually or read from the script/batch file.

It is over time that elements like variables, branching, and finer control over I/O were added to scripting, turning scripts into actual programs. And so it's natural that the default continued to be what made sense for pure scripting, and deviations from that like ECHO OFF and even the @ in front of it to suppress the actual ECHO OFF have to be explicit.