How MS-DOS 6.22 Parses Pipe Operators

This article explains how the MS-DOS 6.22 command interpreter, COMMAND.COM, processes the pipe operator (|) when chaining commands to external text filters like MORE, SORT, and FIND. Because MS-DOS is a single-tasking operating system that cannot run multiple processes concurrently, it cannot implement true inter-process communication pipes found in Unix-like systems. Instead, COMMAND.COM parses the pipeline syntactically and emulates pipe behavior by sequentially executing programs and staging data through temporary intermediate files on disk.

Command-Line Lexical Analysis

When a user submits a command string containing the pipe character (|, ASCII 0x7C), COMMAND.COM intercepts the input buffer before executing any program. During lexical analysis, the shell scans the command line from left to right, treating the pipe character as a delimiter.

Upon detecting a pipe, COMMAND.COM bifurcates the command string into two separate execution entities:

  1. The left-hand command (the data producer).
  2. The right-hand command (the consumer or external filter tool).

If multiple pipe operators are present (such as TYPE FILE.TXT | SORT | MORE), the interpreter breaks the pipeline down into an ordered series of discrete stages.

Temporary File Allocation

Because MS-DOS cannot execute the producer and the consumer simultaneously, it cannot stream data directly through memory buffers. To bridge this gap, COMMAND.COM creates temporary files on the disk to buffer standard output.

  1. Path Resolution: The shell checks environment variables to determine where to store the intermediate file. It prioritizes the TEMP variable, followed by TMP. If neither variable is defined or valid, the files are written directly to the current working directory.
  2. File Generation: COMMAND.COM invokes DOS Interrupt 21h, Function 5Ah (Create Temporary File) to generate a unique, non-colliding filename. Historically, these files often follow patterns such as %TEMP%\_PIPE1.$$$ and %TEMP%\_PIPE2.$$$.

Handle Redirection and Sequential Execution

Once the temporary files are allocated, COMMAND.COM manages standard input (STDIN, Handle 0) and standard output (STDOUT, Handle 1) via DOS file handle manipulation interrupts:

  1. Executing the Producer: COMMAND.COM duplicates the file handle for the first temporary file and redirects STDOUT to point to it using INT 21h, Function 46h (Force Duplicate File Handle). The shell then executes the left-hand command via INT 21h, Function 4Bh (EXEC). The producing program writes its output normally, unaware that its standard output is being written to disk rather than the console screen.
  2. Executing the Filter: After the first process terminates and control returns to COMMAND.COM, the interpreter restores standard handles and prepares the right-hand command. It redirects the consumer's STDIN to the temporary file containing the producer's data. If another pipe follows, the consumer's STDOUT is simultaneously redirected to a second temporary file; otherwise, STDOUT remains assigned to the active display adapter.
  3. Running External Filters: The external filter tool (such as SORT.EXE or MORE.COM) is executed. The filter simply reads from Handle 0 sequentially until it encounters an End-of-File marker (EOF, ASCII 0x1A or file length exhaustion), processes the text, and writes to Handle 1.

Cleanup and Error Handling

Once the downstream filter finishes its execution and returns control to COMMAND.COM, the shell executes cleanup routines:

  • File Deletion: COMMAND.COM invokes INT 21h, Function 41h (Delete File) to erase the intermediate temporary files from the disk.
  • Abnormal Termination: If a user aborts execution using Ctrl+C or Ctrl+Break, MS-DOS attempts to clean up active temporary pipe files during its interrupt routine. However, if a power loss or hard crash occurs during processing, these scratch files remain on disk, requiring manual deletion by the user.