Search code examples
windowsbatch-filecmdprompt

How to modify PROMPT to include variables?


Here is a simplified version of what I need... if I set a variable and set it as my PROMPT, like this:

set myvar=AAA
PROMPT %myvar%$g

gives me:

AAA>

but then, I want to change my variable and consequently the prompt, so:

set myvar=BBB

should produce as my PROMPT:

BBB>

but, it is still

AAA>

How do make it dynamic?


Solution

  • There is no way to directly do that; however, you may easily get an equivalent result if you write your own "shell". For example:

    @echo off
    setlocal
    
    :loop
       echo/
       set "command="
       set /P "command=%myvar%>"
       call %command%
    goto loop
    

    Output example:

    >set myvar=AAA
    
    AAA>echo %time%
    14:42:12.32
    
    AAA>set myvar=BBB
    
    BBB>echo %date%
    10/12/2015
    
    BBB>exit