MS-DOS 6.22 Environment Variables in Shell Escapes
When an application in MS-DOS 6.22 initiates a shell escape by
launching a secondary command processor (COMMAND.COM), the
operating system duplicates the caller's active environment block into a
newly allocated segment in conventional memory. Because MS-DOS treats
this duplicated block as an isolated, transient copy, any environment
variables added, modified, or deleted within the spawned shell are
completely discarded upon termination, leaving the parent application's
original environment unchanged.
The Duplication Mechanism
When an application launches a child shell, it invokes MS-DOS
Interrupt 21h, Function 4Bh (EXEC). The calling program populates an
EXEC parameter block that includes a pointer to an environment segment.
If this pointer is set to 0000h, MS-DOS automatically
copies the parent process's existing environment segment for the
child.
The environment itself is stored in memory as a series of
null-terminated ASCII strings (format KEY=VALUE),
terminated by a final null byte and followed by the fully qualified path
of the executing program. During the EXEC call, MS-DOS allocates a new
memory control block (MCB) for the child process and copies these
strings byte-by-byte into the new segment.
Sizing and Memory Constraints
By default, the primary shell defined in CONFIG.SYS
allocates environment memory according to the /E: switch on
the SHELL= line (defaulting to 256 bytes if unspecified).
However, when a secondary shell is spawned during an escape:
- The new environment block is allocated only enough paragraphs (16-byte units) to hold the inherited strings and the program path, rounded up to the nearest paragraph boundary.
- Unless the parent program explicitly invokes
COMMAND.COM /E:xxxxwith a specified byte size, the child shell receives an environment with minimal free space. - Attempting to define new variables using the
SETcommand within the shell escape frequently results in an "Out of environment space" error due to this snug allocation.
Variable Isolation and Cleanup
Because the child process operates within its own dedicated memory segment, write operations to the environment table do not affect the parent program or the root system environment. There is no inter-process communication or upward propagation mechanism in MS-DOS 6.22.
When the user types EXIT to terminate the shell
escape:
- The secondary
COMMAND.COMissues Interrupt 21h, Function 4Ch to terminate. - MS-DOS reclaims the conventional memory allocated to the child process, including its Program Segment Prefix (PSP), code segment, and environment block, using Interrupt 21h, Function 49h.
- Execution control returns to the parent program, which continues to reference its original, unmodified environment segment.