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:
- The left-hand command (the data producer).
- 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.
- Path Resolution: The shell checks environment
variables to determine where to store the intermediate file. It
prioritizes the
TEMPvariable, followed byTMP. If neither variable is defined or valid, the files are written directly to the current working directory. - File Generation:
COMMAND.COMinvokes 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:
- Executing the Producer:
COMMAND.COMduplicates the file handle for the first temporary file and redirectsSTDOUTto 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. - 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'sSTDINto the temporary file containing the producer's data. If another pipe follows, the consumer'sSTDOUTis simultaneously redirected to a second temporary file; otherwise,STDOUTremains assigned to the active display adapter. - Running External Filters: The external filter tool
(such as
SORT.EXEorMORE.COM) is executed. The filter simply reads from Handle 0 sequentially until it encounters an End-of-File marker (EOF, ASCII0x1Aor 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.COMinvokes INT 21h, Function 41h (Delete File) to erase the intermediate temporary files from the disk. - Abnormal Termination: If a user aborts execution
using
Ctrl+CorCtrl+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.