Windows batch: echo without new line

后端 未结 18 1407
失恋的感觉
失恋的感觉 2020-11-22 04:01

What is the Windows batch equivalent of the Linux shell command echo -n which suppresses the newline at the end of the output?

The idea is to write on t

相关标签:
18条回答
  • 2020-11-22 04:40

    Here is another method, it uses Powershell Write-Host which has a -NoNewLine parameter, combine that with start /b and it offers the same functionality from batch.

    NoNewLines.cmd

    @ECHO OFF
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
    PAUSE
    

    Output

    Result 1 - Result 2 - Result 3 - Press any key to continue . . .
    

    This one below is slightly different, doesn't work exactly like the OP wants, but is interesting because each result overwrites the previous result emulating a counter.

    @ECHO OFF
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
    start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
    PAUSE
    
    0 讨论(0)
  • 2020-11-22 04:41

    Using set and the /p parameter you can echo without newline:

    C:\> echo Hello World
    Hello World
    
    C:\> echo|set /p="Hello World"
    Hello World
    C:\>
    

    Source

    0 讨论(0)
  • 2020-11-22 04:41

    Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.

    0 讨论(0)
  • 2020-11-22 04:42

    Inspired by the answers to this question, I made a simple counter batch script that keeps printing the progress value (0-100%) on the same line (overwritting the previous one). Maybe this will also be valuable to others looking for a similar solution.

    Remark: The * are non-printable characters, these should be entered using [Alt + Numpad 0 + Numpad 8] key combination, which is the backspace character.

    @ECHO OFF
    
    FOR /L %%A in (0, 10, 100) DO (     
        ECHO|SET /P="****%%A%%"    
        CALL:Wait 1
    )
    
    GOTO:EOF
    
    :Wait
    SET /A "delay=%~1+1"
    CALL PING 127.0.0.1 -n %delay% > NUL
    GOTO:EOF
    
    0 讨论(0)
  • 2020-11-22 04:45

    As an addendum to @xmechanix's answer, I noticed through writing the contents to a file:

    echo | set /p dummyName=Hello World > somefile.txt
    

    That this will add an extra space at the end of the printed string, which can be inconvenient, specially since we're trying to avoid adding a new line (another whitespace character) to the end of the string.

    Fortunately, quoting the string to be printed, i.e. using:

    echo | set /p dummyName="Hello World" > somefile.txt
    

    Will print the string without any newline or space character at the end.

    0 讨论(0)
  • 2020-11-22 04:46

    From here

    <nul set /p =Testing testing
    

    and also to echo beginning with spaces use

    echo.Message goes here
    
    0 讨论(0)
提交回复
热议问题