Description
openedon Apr 20, 2023
Windows Terminal version
all
Windows build number
10.0.19041.508
Other Software
No response
Steps to reproduce
- Uncompress windows_terminal_broken_variable_expansion.zip
- Run
correct.bat
contained in the zip file and observe the generated print. - Then run
broken.bat
and observe the generated print.
The zip file contains the following bat scripts:
testscript.bat
@echo off
echo.
:: these seem to work ok
echo Path of current script without quotes: %~0
echo Drive letter of current script: %~d0
echo Filename of current script: %~n0
:: the following all fail
echo Full path of current script: %~f0
echo Path of current script: %~p0
echo File extension of current script: %~x0
echo Short name path of current script: %~s0
echo File attributes of current script: %~a0
echo Date/time of current script: %~t0
echo Byte size of current script: %~z0
echo Drive + Absolute path of current script: %~dp0
echo Filename + extension of current script: %~nx0
echo Full short path name of current script: %~fs0
echo Curdir listing of current script: %~ftza0
correct.bat
@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
testscript
broken.bat
@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
"testscript"
Expected Behavior
Both correct.bat
and broken.bat
should print out exact same output.
Running correct.bat
prints the expected behavior:
E:\code\emsdk\19207>correct.bat
Path of current script without quotes: testscript
Drive letter of current script: E:
Filename of current script: testscript
Full path of current script: E:\code\emsdk\19207\subdir\testscript.bat
Path of current script: \code\emsdk\19207\subdir\
File extension of current script: .bat
Short name path of current script: E:\code\emsdk\19207\subdir\TESTSC~1.BAT
File attributes of current script: --a--------
Date/time of current script: 04/20/2023 02:17 PM
Byte size of current script: 699
Drive + Absolute path of current script: E:\code\emsdk\19207\subdir\
Filename + extension of current script: testscript.bat
Full short path name of current script: E:\code\emsdk\19207\subdir\TESTSC~1.BAT
Curdir listing of current script: --a-------- 04/20/2023 02:17 PM 699 E:\code\emsdk\19207\subdir\testscript.bat
Actual Behavior
broken.bat
will print out incorrect expansion of the shell expression variables ~f0
, %~p0
, %~x0
, %~s0
, %~a0
, %~t0
, %~z0
, %~dp0
, %~nx0
, %~fs0
and %~ftza0
. In that script, the invocation of the test script is enclosed in quotes, which is sometimes necessary e.g. when a path contains spaces in it.
In other words, calling testscript.bat
works, but calling "testscript.bat"
does not.
Running broken.bat
prints the incorrect behavior:
E:\code\emsdk\19207>broken.bat
Path of current script without quotes: testscript
Drive letter of current script: E:
Filename of current script: testscript
Full path of current script: E:\code\emsdk\19207\testscript
Path of current script: \code\emsdk\19207\
File extension of current script:
Short name path of current script: E:\code\emsdk\19207\testscript
File attributes of current script:
Date/time of current script:
Byte size of current script:
Drive + Absolute path of current script: E:\code\emsdk\19207\
Filename + extension of current script: testscript
Full short path name of current script: E:\code\emsdk\19207\testscript
Curdir listing of current script: E:\code\emsdk\19207\testscript
Note the contents of correct.bat
:
@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
testscript
and broken.bat
is identical, but it just encloses the invocation of testscript
in quotes:
@echo off
setlocal
set PATH=%PATH%;%~dp0subdir
"testscript"
which should not have any functional difference, yet the behavior of the scripts differ.