Programming Field

Prompt - DOS/Command Prompt Reference

Changes ‘prompt’ (display such as ‘C:\>’) for the prompt window.


prompt [<string>]
set prompt[=<string>]


<string> Specifies a string. For how to specify, please see Details and Samples. If omitted, the system default setting is applied.

[Windows NT] To see the current setting, use the second syntax (Set statement) without ‘=’ and following strings.

[MS-DOS, Windows 95/98/Me] To see the current setting, type ‘echo %PROMPT%’.


About Prompt

Usage of Prompt

In MS-DOS Prompt/Command Prompt, ‘Prompt’ command changes the ‘prompt’, which is a message that prompts for input. Usually this setting is ‘the current drive and path’ + ‘greater-than sign (>)’, such as ‘C:\path>’, and is based on:

prompt $p$g

‘$p’ means ‘the current drive and path’ and ‘$g’ means ‘a greater-than sign (>)’.

The characters that can be specified for Prompt are all characters that can be used on the command line. Characters that cannot be used on the command line and additional information such as the current path can be specified with special codes beginning with ‘$’ (see Special codes available in Prompt).

In batch files, in most cases the batch programs suppress command line output by putting ‘echo off’ at head, but conversely by putting ‘echo on’ the programs can output commands to be executed (useful for debugging/testing). In this case, usual Prompt setting may make noisy output, such as when the current path is long. To avoid this, you can use Prompt command to change the Prompt setting to adjust output format.

Since Prompt exists as an environment variable, Prompt can be set with Set command. On the other hand, if Prompt is changed in batch programs, its change remains in the current environment (session) like environment variables.

[MS-DOS, Windows 95/98/Me] To use common Prompt setting for all MS-DOS Prompts, add Prompt command to Autoexec.bat. For Windows Me, use the syntax with Set command (the behavior not using Set is unconfirmed).

[Windows NT] To use common Prompt setting for all Command Prompts, add ‘PROMPT’ setting to environment variables settings in Advanced system settings.

[Windows NT] The Prompt setting is subject to ‘localization’ by Setlocal and Endlocal. If changing the Prompt setting after Setlocal and then executing Endlocal or exiting the batch program, the Prompt setting is restored to the one before calling Setlocal.

If the string parameter is omitted, the default setting (following setting) is used.

[MS-DOS, Windows 95/98/Me] The default setting is ‘$n$g’.

[Windows NT?] The default setting is ‘$p$g’.

Special codes available in Prompt

All characters below are case insensitive.

$a [Windows XP or later] Represents an ampersand ‘&’. (In [MS-DOS, Windows 95/98/Me] use ‘&’ directly.)
$b Represents a pipe symbol ‘|’. (This character cannot be used directly unless using ‘^’.)
$c [Windows XP or later] Represents a opening parenthesis ‘(’. (In [MS-DOS, Windows 95/98/Me] use ‘(’ directly.)
$d Represents the current date. The date format depends on the current system locale.
$e Represents an escape character (ASCII code 27, \x1b). In MS-DOS Prompt with ANSI.SYS installed or in Command Prompt, the Prompt setting can be colored or etc. by using escape sequences.
$f [Windows XP or later] Represents a closing parenthesis ‘)’. (In [MS-DOS, Windows 95/98/Me] use ‘)’ directly.)
$g Represents a greater-than sign ‘>’. (This character cannot be used directly unless using ‘^’.)
$h Represents a backspace. If a backspace is included, a character immediately before the backspace will be deleted.
$l Represents a less-than sign ‘<’. (This character cannot be used directly unless using ‘^’.)
$n Represents the current drive letter.
$p Represents the current drive and path.
$q Represents a equal sign ‘=’. (This character is not recognized if in the head of Prompt command parameter (except when using Set).)
$s [Windows XP or later] Represents a space character. (Space characters can be used directly. In [MS-DOS, Windows 95/98/Me] use spaces directly.)
$t Represents the current time. This displays the time including the hundredth of a second.
$v Represents the MS-DOS or Windows version.
$_ Represents a newline (CR+LF).
$$ Represents a dollar sign ‘$’.
$+ [Windows NT series?/XP or later] [Extensions] Expands to ‘+’ symbols according to the number of Pushd execution (stack count). For example, when Pushd is called twice, ‘$+’ will be replaced with ‘++’.
$m [Windows NT series?/XP or later] [Extensions] Expands to the share name (network path) associated to the current drive if the current drive is a network drive. If not a network drive, ‘$m’ will be replaced with an empty string.


All the samples below assume the current directory is ‘C:\foo\bar’. And, the tail ‘_’ character indicates the cursor.

Sample 1

prompt [$d $t] $n$g

By executing this, the prompt will be as follows:

[2010/01/01 01:23:45.67] C>_

If you don't want to display to 1/100th of a second, you can eliminate by using three backspaces ($h) like this:

prompt [$d $t$h$h$h] $n$g

By executing this, the prompt will be as follows:

[2010/01/01 01:23:45] C>_

Sample 2

set prompt=The current directory is $p .$_$g

By executing this, the prompt will be as follows:

The current directory is C:\foo\bar .

Since ‘$_’ means a newline, the characters following ‘$_’ will be displayed on the second line.

Sample 3

prompt [%USERNAME%@%COMPUTERNAME% $p]$$$s

[Windows NT] By executing this, the prompt will be changed to the style like following:

[user@computer C:\foo\bar]$ _

In this case, ‘$s’ can be replaced with a space character directly.

Sample 4 (Batch file)

* In the sample below there is a space immediately after ‘+’ on line 4.

@echo off
if "%DEBUG_MODE%"=="" goto Main
prompt + 
echo on
MyCheck.exe /list %LIST_FILE%
if errorlevel 1 goto Finish
MyBackup.exe /list %LIST_FILE%
@prompt %OLD_PROMPT%

This batch file executes ‘MyCheck.exe’ and ‘MyBackup.exe’, but when the environment variable ‘DEBUG_MODE’ has some value, this batch calls ‘echo on’ to print commands to be executed. In that case, the batch uses Prompt command to set the prefix character of output command to ‘+ ’.

Changed Prompt setting remains after the batch program exits, so the program keeps the old setting to ‘OLD_PROMPT’ and restores at the end. (Since ‘PROMPT’ is available as an environment variable, it can be written as ‘set OLD_PROMPT=%PROMPT%’.)

[Windows NT/2000/XP or later] By using Setlocal it is not necessary to keep the setting to ‘OLD_PROMPT’.

* In ‘echo on’ mode the output of command lines (and preceding characters based on Prompt setting) always follows a newline. This newline cannot be eliminated by Prompt command.
* [MS-DOS, Windows 95/98/Me only] An empty line is treated as a command line, resulting that in ‘echo on’ mode only a string based on Prompt setting will be printed when reaching an empty line. To suppress this, remove empty lines or put ‘@’ character only to the line.