Windows batch: echo without new line

后端 未结 18 1429
失恋的感觉
失恋的感觉 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:46

    Echo with preceding space and without newline

    As stated by Pedro earlier, echo without new line and with preceding space works (provided "9" is a true [BackSpace]).

    <nul set /p=.9    Hello everyone
    

    I had some issues getting it to work in Windows 10 with the new console but managed the following way.
    In CMD type:

    echo .◘>bs.txt
    

    I got "◘" by pressing [Alt] + [8]
    (the actual symbol may vary depending upon codepage).

    Then it's easy to copy the result from "bs.txt" using Notepad.exe to where it's needed.

    @echo off
    <nul set /p "_s=.◘    Hello everyone"
    echo: here
    
    0 讨论(0)
  • 2020-11-22 04:50

    Using: echo | set /p= or <NUL set /p= will both work to suppress the newline.

    However, this can be very dangerous when writing more advanced scripts when checking the ERRORLEVEL becomes important as setting set /p= without specifying a variable name will set the ERRORLEVEL to 1.

    A better approach would be to just use a dummy variable name like so:
    echo | set /p dummyName=Hello World

    This will produce exactly what you want without any sneaky stuff going on in the background as I had to find out the hard way, but this only works with the piped version; <NUL set /p dummyName=Hello will still raise the ERRORLEVEL to 1.

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

    I made a function out of @arnep 's idea:

    echo|set /p="Hello World"

    here it is:

    :SL (sameline)
    echo|set /p=%1
    exit /b
    

    Use it with call :SL "Hello There"
    I know this is nothing special but it took me so long to think of it I figured I'd post it here.

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

    You can suppress the new line by using the set /p command. The set /p command does not recognize a space, for that you can use a dot and a backspace character to make it recognize it. You can also use a variable as a memory and store what you want to print in it, so that you can print the variable instead of the sentence. For example:

    @echo off
    setlocal enabledelayedexpansion
    for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
    cls
    set "var=Hello World! :)"
    set "x=0"
    
    :loop
    set "display=!var:~%x%,1!"
    <nul set /p "print=.%bs%%display%"
    ping -n 1 localhost >nul
    set /a "x=%x% + 1"
    if "!var:~%x%,1!" == "" goto end
    goto loop
    
    :end
    echo.
    pause
    exit
    

    In this way you can print anything without a new line. I have made the program to print the characters one by one, but you can use words too instead of characters by changing the loop.

    In the above example I used "enabledelayedexpansion" so the set /p command does not recognize "!" character and prints a dot instead of that. I hope that you don't have the use of the exclamation mark "!" ;)

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

    Use EchoX.EXE from the terrific "Shell Scripting Toolkit" by Bill Stewart
    How to suppress the linefeed in a Windows Cmd script:

    @Echo Off
    Rem Print three Echos in one line of output
    EchoX -n "Part 1 - "
    EchoX -n "Part 2 - "
    EchoX    "Part 3"
    Rem
    

    gives:

    Part 1 - Part 2 - Part 3
    {empty line}
    d:\Prompt>
    

    The help for this usage is:

    Usage: echox [-n] message
      -n       Do not skip to the next line.
      message  The text to be displayed.
    

    The utility is smaller than 48K, and should live in your Path. More things it can do:
    - print text without moving to the next line
    - print text justified to the left, center, or right, within a certain width
    - print text with Tabs, Linefeeds, and Returns
    - print text in foreground and background colors

    The Toolkit includes twelve more great scripting tricks.
    The download page also hosts three other useful tool packages.

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

    You can remove the newline using "tr" from gnuwin32 (coreutils package)

    @echo off
    set L=First line
    echo %L% | tr -d "\r\n"
    echo Second line
    pause
    

    By the way, if you are doing lots of scripting, gnuwin32 is a goldmine.

    0 讨论(0)
提交回复
热议问题