简体   繁体   中英

Please explain following outputs in Windows command prompt

I tried to customize windows command prompt with the following batch file.

@echo off
cls
:cmd
set /p "cmd=%cd%>"
%cmd%
goto cmd

So, when I open the batch file, it just takes my command into cmd variable and executes it and again prompts for a new command.

But the following command echo %cd% outputs only %cd%

Then I enabled delayedexpansion and used echo !cd! and got the desired output.

在此输入图像描述

I think, because of the delayed expansion, cmd variable now holds echo c:\\Users\\Sourav\\Desktop ( am I correct? )

But I got confused when I tried to open the command prompt (not the batch file) and tried the following commands.

在此输入图像描述

I thought, I will get c:\\Users\\Sourav\\Desktop but I got !cd! . This contradicts my understanding of how echo !cd! is working in first case.

Why am I getting different output in the second case?

Can anyone suggest any improvement to the batch file, so that I can get desired output just using echo %cd% in first case?

you need another level of parsing. You can use call to do so:

@echo off
cls
:cmd
set /p "cmd=%cd%>>"
call %cmd%
goto cmd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM