Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:09:12+00:00 2026-06-05T09:09:12+00:00

While answering this question I found some strange behavior for which I have no

  • 0

While answering this question I found some strange behavior for which I have no explanation

for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echo %z^)') do @echo %a0

You’ll see numbers 10..100, now just add the pipe, e.g. to sort or more, whatever:

for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echo %z^)^|sort') do @echo %a0

There’ll be spaces added between %a and 0!
Looks like echo-ing something through a pipe adds a trailing space, it can be easily seen:

>_tempfile echo no space here
>_tempfile echo and here's a space|more

and even

>_tempfile <nul set /p =also a space|sort

(probably uses echo to print the prompt)

This doesn’t happen when there’s no output redirection (whether into a file or for command).
Is it a bug or am I missing something? How do I get rid of the space? (besides the dirty hack of stripping the last character with var:~0,-1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:09:15+00:00Added an answer on June 5, 2026 at 9:09 am

    Excellent and interesting question (+1)

    The space is introduced by the pipe mechanism of the CMD parser, not by SORT.

    When you execute a command using FOR /F, the command is executed in its own CMD shell. Also, each side of a pipe is executed in its own CMD shell. See Why does delayed expansion fail when inside a piped block of code? for more info.

    So your command actually instantiates 3 CMD shells, one for the FOR /F command, which in turn creates 2 for each side of the pipe.

    You can see how the commands get parsed and fed into the CMD shell using the %CMDCMDLINE% dynamic variable. Because we are executing the command from the command line, we need to escape at least one character in the variable name twice so that it doesn’t get expanded until it reaches the inner most CMD shell.

    Here is the command with the results (the leading > is my command prompt):

    >for /f "delims=" %a in ('(echo %^^^cmdcmdline%^&for /l %z in (1,1,10^) do @echo %z^)^|sort') do @echo %a0
    1 0
    10 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0
    C:\Windows\system32\cmd.exe  /S /D /c" ( echo %cmdcmdline% & FOR /L %z in (1 1 10) do @ echo %z )" 0
    

    The last line of output is the command line used for the left side of the pipe. You can see how the parser added spaces in a number of places.

    You can circumvent the problem by using a simple batch script to echo the value instead of the ECHO command.

    echoArgs.bat

    @echo(%*
    

    Now when you run this command you get the desired result

    >for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echoArgs %z^)^|sort') do @echo %a0
    10
    100
    20
    30
    40
    50
    60
    70
    80
    90
    

    Another method to circumvent the problem is to create a variable with your ECHO command and escape the expansion of the variable appropriately.

    >set cmd=@(echo %z)
    
    >for /f "delims=" %a in ('(for /l %z in (1,1,10^) do %^^^cmd%^)^|sort') do @echo %a0
    10
    100
    20
    30
    40
    50
    60
    70
    80
    90
    

    EDIT

    The >_tempfile echo and here's a space|more example is also interesting. There is an extra space at the end of the output file. But Chad Nouis is correct that nothing is being sorted because of the redirection of the left side. Any command could be used on the right side and the result would be the same.

    The source of the problem is still the parser, but the way the parser restructures the commmand is interesting.

    >>_tempfile echo %^cmdcmdline%|rem
    
    >type _tempfile
    C:\Windows\system32\cmd.exe  /S /D /c" echo %cmdcmdline% 1>_tempfile"
    

    Notice how the redirection is moved from the beginning to the end of the command, and the file handle of 1 is explicitly added. You can certainly see where the extra space comes from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While answering this question I noticed that I have never come across any property
While answering this question DOM parser: remove certain attributes only I noticed that some
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output
While answering this question C# Regex Replace and * the point was raised as
While answering this question regarding safe escaping of filename with spaces (and potentially other
While answering this question I noticed that I got the following dialog while atempting
While answering this question I came across an interesting difference in the overload resolution
So, I was playing around with Python while answering this question , and I
In looking at this question which Jon did a fine job in answering... '
(I have just answered this question after searching for a while, so I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.