Page 1 of 1
caret removes the first command after opening parenthesis if its concat'd to the first token of command without space
Posted: 21 Jun 2020 06:32
by lockedscope
- Following works because there is space after the first token of command
- Following removes the first command because of caret is in first token and echo works
Code: Select all
(
di^
r t*.cmd
echo abc
)
'r' is not recognized as an internal or external command,
operable program or batch file.
abc
- Following works, caret is in second token
- Following does not work, caret is concatenated to the first token
Code: Select all
(
dir^
t*.cmd
)
't*.cmd' is not recognized as an internal or external command, operable program or batch file.
- When there is a space, it is treated as a command and error raised and dir works fine.
Code: Select all
(
^<space>
dir ^
t*.cmd
)
' ' is not recognized as an internal or external command,
operable program or batch file.
<DIR RESULT>
- And weirdly
Code: Select all
(
^
<space>
dir ^
t*.cmd
)
The syntax of the command is incorrect.
So, what causes it, is it a bug, are there any rules covering it?
Re: caret removes the first command after opening parenthesis if its concat'd to the first token of command without spac
Posted: 22 Jun 2020 00:20
by jeb
Hi lockedscope,
nice finding
I didn't knew that and never tested it.
The rule seems to be:
- In paremthesis:
- If a caret+line feed splits the command token (usually the first token), then only the part after the last LF is used
- This rule is true for any position in the parenthsis, not only the first line
Simple exsample
More complex examples
Code: Select all
@echo on
cls
(
echo Start
^echo #1: Works
^
echo #2: Works
ec^HO #3: Fails
ec^
HO #4: Fails
<nul echo #5: Works
<n^
ul echo #6: Works
<nul ec^
ho #7: FAILS
<n^
ul ec^
ho #8: FAILS
^<nul echo #9: WORKS
)
echo End
Test 5-8 shows that the rule only applies for a command token, but not for first redirection!
Test 9 shows the (already known,
Bug/Mystery in the phase parsing rules 1.5 and 2 CR vs redirect) strange behaviour, that the "nul" token is removed and the "echo" is used for the redirection.
Really strange, because there shouldn't no redirection at all, as the redirection character was escaped
jeb
Re: caret removes the first command after opening parenthesis if its concat'd to the first token of command without spac
Posted: 22 Jun 2020 00:43
by jeb
Btw. the @ has also sme impact.
Code: Select all
@echo on
cls
(
echo Start
@ ec^
ho #1: Works
@ec^
ho #2: Fails
@@^
@@ ec^
ho #3: Works
xyz@@^
@@ ec^
ho #4: Works
)
echo End
Re: caret removes the first command after opening parenthesis if its concat'd to the first token of command without spac
Posted: 22 Jun 2020 10:06
by lockedscope
As a workaround, following works with a label operator(: colon) before the line wherever `commands first token splitting caret` usage appears
The rule applies at any line so we need to fix it before every line such a weird caret usage appears
Code: Select all
(:
di^
r t*.cmd
echo a
:
di^
r t*.cmd
echo b)
Re: caret removes the first command after opening parenthesis if its concat'd to the first token of command without spac
Posted: 25 Jun 2020 14:34
by lockedscope
Another workaround is to redefine parenthesis

;
- opening parenthesis with the escaped (!& and
- a command delimiter - with the escaped !&
Rules:
- First command could be on the same line as opening parenthesis or on the next line without any space between.
- It works when there is a space between command delimeter and command but fails when command is on the next line.
Code: Select all
setlocal EnableDelayedExpansion
set t=before
set "(=^(^!^&^^"
set "-=^!^&"
set )=^)
%(%di^
r te^
mp*.cmd
%-%di^
r m^
*.cmd
set t=4
echo percent: %t%
echo delayed: !t!
echo b
%)%
echo after parenthesis - percent: %t%
(echo ^
Other parenthesis work fine )
endlocal