Programming Field

COMSPEC environment variable - DOS/Command Prompt Reference

COMSPEC environment variable contains the path to the command interpreter (Command.com or Cmd.exe), and is used mainly by the system.

>echo %COMSPEC%
C:\WINDOWS\system32\cmd.exe

Details

Usage of COMSPEC

The value of COMSPEC is used as the default program for displaying the prompt or the interpreter program for batch file execution. Therefore, the COMSPEC program must be able to process commands and batch files.

[MS-DOS, Windows 95/98] Normally the command interpreter specified in Config.sys ‘Shell’ statement sets COMSPEC environment variable. If the interpreter is Command.com, COMSPEC value will be the absolute path of ‘Command.com’ existing in the path specified by the first parameter of Command.com in the Shell statement. This value is used as a search path for re-running the prompt when Command.com is unloaded by executing any programs. If setting invalid value to COMSPEC on Command.com, and executing any programs, the system halt with message ‘Cannot load COMMAND, system halted’ on terminating the program. (You can use /P option to Command.com in the Config.sys Shell statement not to unload.)

[Windows NT?/XP or later] COMSPEC is set to ‘%SystemRoot%\system32\cmd.exe’ during Windows installation, but can be changed. By changing this value, an original interpreter can always process any batch files, but in most cases COMSPEC is assumed to refer to Windows batch file interpreter (cmd.exe), so if COMSPEC value is not cmd.exe, batch files may not work properly. If COMSPEC is not set, the system setting value is used, and if it does not exist, ‘%SystemRoot%\system32\cmd.exe’ is used.

* In Windows batch files (*.bat, *.cmd) can be passed to Win32API CreateProcess function as an execution program. CreateProcess uses COMSPEC value on the current environment to execute batch files with the parameter ‘/C’ or ‘/K’ option, the batch file, and rest parameters. (This is why ‘open’ command of .bat and .cmd file type in the registry is ‘"%1" %*’.) However, if the batch file is executed in ‘cmd.exe’, its cmd.exe will interpret the batch file.

COMSPEC environment variable is usually used to retrieve the command interpreter path like following sample. COMSPEC value is rarely changed in batch files or etc.

Samples

Sample

%COMSPEC% /K setenv.bat

Executes the command interpreter with the parameter ‘/K setenv.bat’.

If COMSPEC is Command.com or Cmd.exe, this command means ‘executes setenv.bat batch file and continues the prompt’.