MS-DOS 6.22 Out of Environment Space in Batch Files
When running batch files in MS-DOS 6.22, attempting to define or
modify variables beyond allocated memory boundaries triggers the "Out of
environment space" error. This article examines how the MS-DOS command
interpreter (COMMAND.COM) handles memory exhaustion during
batch execution, the resulting behavior on subsequent script logic, and
the mechanics required to permanently expand the available environment
buffer.
In MS-DOS 6.22, the master environment block is an allocated memory
segment dedicated to storing system variables such as PATH,
PROMPT, and user-defined variables set via the
SET command. By default, COMMAND.COM reserves
a static 256 bytes for this table. Because this allocation resides in
conventional memory directly preceding command-line utilities, it cannot
dynamically expand once other programs or drivers have loaded adjacent
to it.
When a batch file executes a SET command that exceeds
the remaining free bytes of this block, MS-DOS handles the event with
specific, non-terminating behaviors:
- Error Output: The interpreter prints
Out of environment spaceto standard output. - Failed Variable Assignment: The requested variable is either completely discarded or truncated, depending on whether any contiguous bytes remain to store the variable name and delimiter.
- Uninterrupted Execution: MS-DOS does not halt the batch file. Execution immediately moves to the very next line in the script.
- No
ERRORLEVELFlag: The internalSETcommand does not return an exit code to indicate failure. Consequently, routines utilizingIF ERRORLEVELcannot detect that the environment variable failed to register.
This non-halting behavior frequently leads to cascading script
failures. Because MS-DOS batch processing evaluates undefined variables
as empty strings, subsequent operations referencing
%VARIABLE% fail unpredictably. For example, a conditional
statement structured as IF %MYVAR%==TRUE GOTO END expands
to IF ==TRUE GOTO END, causing MS-DOS to throw a
Syntax error and abort that specific command while
continuing execution down unintended code paths. Similarly, file system
commands relying on directory path variables may resolve to root or
relative paths, inadvertently overwriting or deleting files in
unintended directories.
The problem is exacerbated when batch files invoke secondary command
processors. Using COMMAND /C SCRIPT.BAT creates a child
environment that is typically shrunk to fit only the active variables
inherited from the parent shell, leaving zero to minimal headroom for
any new variables declared inside the sub-script.
To permanently prevent environment exhaustion in MS-DOS 6.22, the
environment block must be resized at boot time. This is managed via the
SHELL directive in CONFIG.SYS:
SHELL=C:\DOS\COMMAND.COM C:\DOS\ /E:1024 /P
The /E: parameter dictates the environment size in
bytes, accepting values ranging from 160 up to 32,768. The
/P switch ensures that COMMAND.COM remains
permanent in memory, preventing the user from accidentally exiting the
top-level shell. Setting an allocation such as 1024 or 2048 bytes
provides ample overhead for complex batch files, eliminating runtime
environment space failures entirely.