Rob van der Woude's Scripting Pages

A bug in CMD.EXE's Delayed Variable Expansion?

A message I got from Scott Wegner:

[...] I was curious if you were aware of a particular bug that I happened across today. It involves delayed variable expansion, code blocks, and the pipe operator. Take for example, the code below:

@echo off
setlocal enabledelayedexpansion
set myvar=value
(
	echo.!myvar!
) | more
endlocal

When running this script, you would expect to see "value" printed, but instead, you see "!myvar!". It looks like there is an incompatibility when using these three features together. It is a very specific situation, but I hadn't seen it documented anywhere before.

Is this indeed a bug?

I tested various alternatives, and I think Scott may indeed have found a real bug here.
It is indeed very specific but consistent: only when delayed variable expansion is used inside a code block (one or several commands grouped between parentheses) whose output is piped, the variable expansion is skipped.


page last modified: 2016-09-19; loaded in 0.0068 seconds