Ignores IF statements in batch #20016

Closed
opened 2026-01-31 07:00:46 +00:00 by claunia · 16 comments
Owner

Originally created by @jredfox on GitHub (Jun 1, 2023).

Windows Terminal version

1.16.230126001

Windows build number

10.0.19045.2965

Other Software

No response

Steps to reproduce

make sure to start windows terminal in command prompt.

@Echo off
set c=%~1%
IF NOT "%c%" == "" (
  set c=%c:$=;%
  echo=%c%
)
pause

command:

C:\Users\jredfox\AppData\Roaming\OpenTerminal\scripts\boot\boot.bat "55$23"

output:

55$23

The issue is pretty clear. WT ignores statements inside of IF statements. I run the same batch and command through conHost.exe and it has no issues changing the $ to ;

Expected Behavior

changing the $ to ;

Actual Behavior

does nothing

Originally created by @jredfox on GitHub (Jun 1, 2023). ### Windows Terminal version 1.16.230126001 ### Windows build number 10.0.19045.2965 ### Other Software _No response_ ### Steps to reproduce make sure to start windows terminal in command prompt. ``` @Echo off set c=%~1% IF NOT "%c%" == "" ( set c=%c:$=;% echo=%c% ) pause ``` command: ``` C:\Users\jredfox\AppData\Roaming\OpenTerminal\scripts\boot\boot.bat "55$23" ``` output: ``` 55$23 ``` The issue is pretty clear. WT ignores statements inside of IF statements. I run the same batch and command through `conHost.exe` and it has no issues changing the `$` to `;` ### Expected Behavior changing the `$` to `;` ### Actual Behavior does nothing
claunia added the Needs-TriageIssue-BugResolution-No-ReproProduct-Cmd.exe labels 2026-01-31 07:00:46 +00:00
Author
Owner

@237dmitry commented on GitHub (Jun 1, 2023):

Your batch file is incorrect. Use setlocal enabledelayedexpansion to set and echo vsriable in one script block.

@Echo off
setlocal enabledelayedexpansion

set "c=%~1%"
IF NOT "%c%"=="" (
  set "c=%c:$=;%"
  echo !c!
)
pause
@237dmitry commented on GitHub (Jun 1, 2023): Your batch file is incorrect. Use `setlocal enabledelayedexpansion` to set and echo vsriable in one script block. ``` @Echo off setlocal enabledelayedexpansion set "c=%~1%" IF NOT "%c%"=="" ( set "c=%c:$=;%" echo !c! ) pause ```
Author
Owner

@DHowett commented on GitHub (Jun 2, 2023):

This is really curious! Thanks for filing it!

The issue is pretty clear. WT ignores statements inside of IF statements.

Just to level-set, WT does not change the behavior of CMD.exe. It is not parsing batch files or processing user input or spawning subshells or reading scripts--it only provides a new UI for CLI applications.

I am also unable to reproduce this!

image

Top - terminal; bottom - conhost; right (for verification) - your batch file.

@DHowett commented on GitHub (Jun 2, 2023): This is really curious! Thanks for filing it! > The issue is pretty clear. WT ignores statements inside of IF statements. Just to level-set, _WT does not change the behavior of CMD.exe_. It is not parsing batch files or processing user input or spawning subshells or reading scripts--it only provides a new UI for CLI applications. I am also unable to reproduce this! ![image](https://github.com/microsoft/terminal/assets/189190/e53349ae-553a-4f67-a47e-9b910b0683a8) Top - terminal; bottom - conhost; right (for verification) - your batch file.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 6, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@jredfox commented on GitHub (Jun 17, 2023):

This is really curious! Thanks for filing it!

The issue is pretty clear. WT ignores statements inside of IF statements.

Just to level-set, WT does not change the behavior of CMD.exe. It is not parsing batch files or processing user input or spawning subshells or reading scripts--it only provides a new UI for CLI applications.

I am also unable to reproduce this!

image

Top - terminal; bottom - conhost; right (for verification) - your batch file.

$ never got replaced to ; in the output it says 55$23 and not 55;23 aka it never ran inside the if statement.

@jredfox commented on GitHub (Jun 17, 2023): > This is really curious! Thanks for filing it! > > > The issue is pretty clear. WT ignores statements inside of IF statements. > > Just to level-set, _WT does not change the behavior of CMD.exe_. It is not parsing batch files or processing user input or spawning subshells or reading scripts--it only provides a new UI for CLI applications. > > I am also unable to reproduce this! > > ![image](https://user-images.githubusercontent.com/189190/242985609-e53349ae-553a-4f67-a47e-9b910b0683a8.png) > > Top - terminal; bottom - conhost; right (for verification) - your batch file. **$** never got replaced to **`;`** in the output it says `55$23` and not `55;23` aka it never ran inside the if statement.
Author
Owner

@jredfox commented on GitHub (Jun 17, 2023):

Edit: the bug now also happens with just cmd.exe. as you can see the value of c doesn't change until after the if statement has executed. Could you report this to cmd.exe people then as it's related to windows terminal?

@Echo off
set c=%~1%
IF NOT "%c%" == "" (
  set c=%c:$=;%
  echo=%c%
)
echo=%c%
@jredfox commented on GitHub (Jun 17, 2023): Edit: the bug now also happens with just `cmd.exe`. as you can see the value of c doesn't change until after the if statement has executed. Could you report this to `cmd.exe` people then as it's related to windows terminal? ``` @Echo off set c=%~1% IF NOT "%c%" == "" ( set c=%c:$=;% echo=%c% ) echo=%c% ```
Author
Owner

@237dmitry commented on GitHub (Jun 17, 2023):

as you can see the value of c doesn't change until after the if statement has executed.

You tried to set and view variable changes inside the same script-block (...). This is not working in this way (and never worked).

set /?
setlocal /?

Or if you do not want to use setlocal (disliked my answer) you have to use call instead.

@Echo off
set c=%~1%
IF NOT "%c%" == "" (
  set c=%c:$=;%
  call echo %%c%%
)
echo %c%
@237dmitry commented on GitHub (Jun 17, 2023): > as you can see the value of c doesn't change until after the if statement has executed. You tried to set and view variable changes inside the same script-block `(...)`. This is not working in this way (and never worked). ``` set /? setlocal /? ``` Or if you do not want to use `setlocal` (disliked my answer) you have to use `call` instead. ``` @Echo off set c=%~1% IF NOT "%c%" == "" ( set c=%c:$=;% call echo %%c%% ) echo %c% ```
Author
Owner

@DHowett commented on GitHub (Jun 19, 2023):

We're also the cmd.exe people! I can definitely assure you that Terminal cannot change the behavior of cmd outside of interactive mode 😄

It is worth noting that it absolutely did run the code inside the IF block, because that is where it is echoing the contents of %c%.

If it was not running the code inside the IF block, there would be no output.

The code as you've written it requires delayed expansion to be turned on.

Can you share the output of reg query "HKLM\Software\Microsoft\Command Processor"?

@DHowett commented on GitHub (Jun 19, 2023): We're also the cmd.exe people! I can definitely assure you that Terminal cannot change the behavior of cmd outside of interactive mode :smile: It is worth noting that it _absolutely did_ run the code inside the `IF` block, because that is where it is echoing the contents of `%c%`. If it was not running the code inside the `IF` block, there would be no output. The code as you've written it requires delayed expansion to be turned on. Can you share the output of `reg query "HKLM\Software\Microsoft\Command Processor"`?
Author
Owner

@DHowett commented on GitHub (Jun 19, 2023):

I would also like to see reg query "HKCU\Software\Microsoft\Command Processor" (the distinction being of course that one is systemwide and one is user-local)

@DHowett commented on GitHub (Jun 19, 2023): I would also like to see `reg query "HKCU\Software\Microsoft\Command Processor"` (the distinction being of course that one is systemwide and one is user-local)
Author
Owner

@microsoft-github-policy-service[bot] commented on GitHub (Jun 23, 2023):

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@microsoft-github-policy-service[bot] commented on GitHub (Jun 23, 2023): This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for **4 days**. It will be closed if no further activity occurs **within 3 days of this comment**.
Author
Owner

@jredfox commented on GitHub (Jun 24, 2023):

Capture
I did install visual studio since this issue has opened.

@jredfox commented on GitHub (Jun 24, 2023): ![Capture](https://github.com/microsoft/terminal/assets/9736369/1ee8cbee-68f9-45ec-983e-2bcb7890a1e5) I did install visual studio since this issue has opened.
Author
Owner

@jredfox commented on GitHub (Jun 24, 2023):

I would also like to see reg query "HKCU\Software\Microsoft\Command Processor" (the distinction being of course that one is systemwide and one is user-local)

Im not able to reproduce it since I installed visual studio 2022. So I just am going to let you guys debug this on a non devloper pc's and try to produce the error again.

for my concern I changed by batch script to this just in case the issue ever does still occur

@Echo off
set c=%~1%
set c=%c:$=;%
IF NOT "%c%" == " " (
  echo=%c%
  cls
)

on an unrelated note I am developing my cross platform way to launch consoles. I noticed Microsoft's interest in Windows Terminal and was wondering who do I contact if they would be interested in my project OpenTerminal? It basically allows you to have a CLI for programs on windows, macOs, linux and is currently for java. But you can also transform it to c++ or c# easily. still in dev phase but could be completed within the next month.

@jredfox commented on GitHub (Jun 24, 2023): > I would also like to see `reg query "HKCU\Software\Microsoft\Command Processor"` (the distinction being of course that one is systemwide and one is user-local) Im not able to reproduce it since I installed visual studio 2022. So I just am going to let you guys debug this on a non devloper pc's and try to produce the error again. **for my concern I changed by batch script to this just in case the issue ever does still occur** ``` @Echo off set c=%~1% set c=%c:$=;% IF NOT "%c%" == " " ( echo=%c% cls ) ``` on an unrelated note I am developing my cross platform way to launch consoles. I noticed Microsoft's interest in Windows Terminal and was wondering who do I contact if they would be interested in my project OpenTerminal? It basically allows you to have a CLI for programs on windows, macOs, linux and is currently for java. But you can also transform it to c++ or c# easily. still in dev phase but could be completed within the next month.
Author
Owner

@zadjii-msft commented on GitHub (Jul 12, 2023):

Alrighty so we can't repro this anymore, and this is seemingly gone. Not sure there's much else to do here. cmd.exe is dark and full of terrors.

@zadjii-msft commented on GitHub (Jul 12, 2023): Alrighty so we can't repro this anymore, and this is seemingly gone. Not sure there's much else to do here. cmd.exe is dark and full of terrors.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20016