Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to echo a line WITHOUT linefeed/newline at the end?

12,407 views
Skip to first unread message

Dennis Halver

unread,
Jul 1, 2009, 3:19:55 PM7/1/09
to
Normally when I execute an "echo" command then automatically an CR/newline/linefeed
is appended. Is there a way to omit/avoid this CR/linefeed?

Dennis

Todd Vargo

unread,
Jul 1, 2009, 4:37:06 PM7/1/09
to

This is answered in "Batch file information and FAQs" which was posted by
Prof. Timo Salmi just 4 days ago.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Auric__

unread,
Jul 1, 2009, 7:10:17 PM7/1/09
to

Covered in the FAQ: "Is it possible to echo without linefeed like the Unix
echo -n?"
http://www.netikka.net/tsneti/info/tscmd015.htm

If you don't want to use any of the several solutions listed there, you can
use this, either by compiling it or using an interpreter (although I'd skip
QBASIC for this):

-----begin echonocr.bas-----
PRINT COMMAND$;
-----end echonocr.bas-----

Alternately alternately, you can use this:

-----begin echonocr.c-----
#include <stdio.h>

int main (int argc, char *argv[]) {
if (argc > 1) {
printf ("%s", argv[1]);
if (argc > 2) {
for (int i=2; i<argc; i++) {
printf (" %s", argv[i]);
}
}
}
}
-----end echonocr.c-----

Compile that with your C compiler of choice. It should work pretty much
anywhere. (It's good in DOS, Windows, and Linux, along with several other
OS's you've probably never heard of.)

--
Words remain my only escape.

Auric__

unread,
Jul 1, 2009, 7:11:46 PM7/1/09
to
On Wed, 01 Jul 2009 20:37:06 GMT, Todd Vargo wrote:

> Dennis Halver wrote:
>> Normally when I execute an "echo" command then automatically an
> CR/newline/linefeed
>> is appended. Is there a way to omit/avoid this CR/linefeed?
>
> This is answered in "Batch file information and FAQs" which was posted by
> Prof. Timo Salmi just 4 days ago.

Ah, I love the Cox servers. Your response appeared *immediately* after I
posted mine.

--
Lost within my plans for life, it all seems so unreal.

Tim Meddick

unread,
Jul 1, 2009, 7:16:45 PM7/1/09
to
Download the small but powerful XECHO.COM utility.

Then the command :

xecho #P

Will Not include the CR+LF linefeed 'characters'

Available at :

http://hp200lx.net/cgi-bin/count-redir.cgi?dbname=xecho133.zip&URL=http://www.hp200lx.net/anonftp/pub/xecho133.zip

==

Cheers, Tim Meddick, Peckham, London. :-)


"Dennis Halver" <ha...@persrec.com> wrote in message
news:4a4bb6db$0$32663$9b4e...@newsspool2.arcor-online.net...

01MDM

unread,
Jul 1, 2009, 10:47:02 PM7/1/09
to

0>nul set /p="String without \n"

Harry Potter

unread,
Jul 2, 2009, 12:40:51 PM7/2/09
to

I don't think so.
-----------------
Joseph Rose, a.k.a. Harry Potter
Working magic in the computer community...or at least striving to! :(

\Rems

unread,
Jul 2, 2009, 4:02:56 PM7/2/09
to

Try this

<nul Set/p=Hi Dennis


\Rems

T Lavedas

unread,
Jul 7, 2009, 12:01:20 PM7/7/09
to

In the more recent WinNT derived OSs, this is often done using a
characteristic of the SET statement with its /P switch. as in ...

set /p dummy="This is text to be echoed without a newline. " < nul
echo For example.

It can also be done by 'escaping' the end-of-line with the standard
escape character, the carat (^), as in ...

echo Beginning of line ^
echo end of line

In older OSs, like DOS and Win9x/Me, it is not particularly easy and
is messy. There are even little utilities around to provide this
capability. I suspect you want one of the solutions above, so I'll
leave it at that.

Tom Lavedas
***********

Lean234

unread,
Jul 26, 2009, 1:53:53 AM7/26/09
to

Yeah I wish I knew. In unix under bash echo -n does the job

Lean234

unread,
Jul 26, 2009, 1:55:55 AM7/26/09
to
On Jul 2, 5:19 am, h...@persrec.com (Dennis Halver) wrote:

Yeah I wish I knew. The bash shell (unix) has echo -n for that.

Timo Salmi

unread,
Jul 26, 2009, 1:56:09 AM7/26/09
to
In article <b7507846-3c53-467b...@v37g2000prg.googlegroups.com>,

Lean234 <wot...@gmail.com> wrote:
> On Jul 2, 5:19�am, h...@persrec.com (Dennis Halver) wrote:
> > Normally when I execute an "echo" command then automatically an CR/newline/linefeed
> > is appended. Is there a way to omit/avoid this CR/linefeed?

> Yeah I wish I knew. In unix under bash echo -n does the job

FAQ

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm


Timo Salmi

unread,
Jul 26, 2009, 1:08:14 PM7/26/09
to
> > Lean234 �<wot...@gmail.com> wrote:
> > > On Jul 2, 5:19�am, h...@persrec.com (Dennis Halver) wrote:
> > > > Normally when I execute an "echo" command then automatically an CR/newline/linefee
> d
> > > > is appended. Is there a way to omit/avoid this CR/linefeed?
> > > Yeah I wish I knew. �In unix under bash �echo -n does the job

> >
> > FAQ
> >
> > � �All the best, Timo
> >
> > --
> > Prof. Timo Salmi � mailto:t...@uwasa.fi � �ftp &http://garbo.uwasa.fi/

> > Hpage:http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
> > Department of Accounting and Finance, � University of Vaasa, �Finland
> > Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.htm

> Um could you give me a link to the FAQ.

As per the above
"Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.htm"

(Please kindly solely use newsgroups, not email, for programming and
similar questions you may have to me.)

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/

Home page: http://www.uwasa.fi/laskentatoimi/henkilokunta/salmitimo/


Department of Accounting and Finance, University of Vaasa, Finland

Timo's FAQ materials at http://lipas.uwasa.fi/~ts/http/tsfaq.html


Message has been deleted

Robert Riebisch

unread,
Jul 31, 2009, 7:03:11 AM7/31/09
to
Dennis Halver wrote:

> Normally when I execute an "echo" command then automatically an CR/newline/linefeed
> is appended. Is there a way to omit/avoid this CR/linefeed?

http://groups.google.de/group/comp.os.msdos.programmer/msg/ea17aa0abbee737e
ftp://garbo.uwasa.fi/pc/batchutil/echon.zip
http://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs_freeware/dosmisc.zip/ECHON.EXE

--
Robert Riebisch
Bitte NUR in der Newsgroup antworten!
Please reply to the Newsgroup ONLY!

BigJoe

unread,
Jan 11, 2013, 7:09:56 AM1/11/13
to
Op woensdag 1 juli 2009 21:19:55 UTC+2 schreef Dennis Halver het volgende:
> Normally when I execute an "echo" command then automatically an CR/newline/linefeed
> is appended. Is there a way to omit/avoid this CR/linefeed?
>
> Dennis

This batch file does the trick:

@echo off
echo | set /p x=This line
echo | set /p x=has no CR-LFs


Output:
This linehas no CR-LFs

Tom Del Rosso

unread,
Jan 11, 2013, 11:29:22 AM1/11/13
to
I expected the side-effect of setting variable x to 'Echo is off.'.

Why doesn't it do that?


--

Reply in group, but if emailing add one more
zero, and remove the last word.


Frank Westlake

unread,
Jan 11, 2013, 11:51:37 AM1/11/13
to
On 2013-01-11 08:29, Tom Del Rosso wrote:
> BigJoe wrote:
>> @echo off
>> echo | set /p x=This line
>> echo | set /p x=has no CR-LFs
>>
>>
>> Output:
>> This linehas no CR-LFs
>
> I expected the side-effect of setting variable x to 'Echo is off.'.

We normally use disable input by redirecting it from NUL:

SET /P "=This line"<NUL:

I guess his code above also effectively disables input by giving it
input that it can't use.

Frank

Tom Del Rosso

unread,
Jan 13, 2013, 11:15:27 PM1/13/13
to
The 'Echo is off.' goes through standard output, not the error stream....

No CR LF is output before the string....

...so why else might set/p be unable to use it?

Frank Westlake

unread,
Jan 14, 2013, 4:24:25 AM1/14/13
to
On 2013-01-13 20:15, Tom Del Rosso wrote:
> Frank Westlake wrote:
>> On 2013-01-11 08:29, Tom Del Rosso wrote:
>>> BigJoe wrote:
>>>> @echo off
>>>> echo | set /p x=This line
>>>> echo | set /p x=has no CR-LFs
>>>>
>>>>
>>>> Output:
>>>> This linehas no CR-LFs
>>>
>>> I expected the side-effect of setting variable x to 'Echo is off.'.
>>
>> We normally use disable input by redirecting it from NUL:
>>
>> SET /P "=This line"<NUL:
>>
>> I guess his code above also effectively disables input by giving it
>> input that it can't use.
>
> The 'Echo is off.' goes through standard output, not the error stream....
>
> No CR LF is output before the string....
>
> ...so why else might set/p be unable to use it?

It seems that CMD will not internally redirect input through a pipe to
SET/P, but it will pass previously redirected input to it. In the BigJoe
script above the input has been redirected internally and CMD will not
give it to SET/P, but since input has been redirected there is no input
for SET/P to read and it is effectively the same as redirecting from NUL:.

Frank

Konrad Kullig

unread,
Jan 21, 2013, 8:31:42 AM1/21/13
to
What if you want to output only spaces?

"Tom Del Rosso" <tom...@verizon.net.invalid> schrieb im Newsbeitrag
news:kcpfj9$uon$1...@dont-email.me...

Tom Lavedas

unread,
Jan 21, 2013, 9:28:49 AM1/21/13
to
On Monday, January 21, 2013 8:31:42 AM UTC-5, Konrad Kullig wrote:
>
> What if you want to output only spaces?
>
By "spaces", do you realy mean black lines (carriage return-linefeeds with no text)? Or do you mean a line with just spaces on it?

Either way, the approach is very similar.

echo.[cr/lf]

or

echo.[spaces][cr/lf]

This is the 'documented' aproach. However, people has experienced unusual behavior in the unlikely situation of having inadvertently created a file named "ECHO" in the directory from which the batch procedure is launched. In such an unlikely situation, the following error is reported ...

'echo.' is not recognized as an internal or external command,
operable program or batch file.

Therefore, as a precaution, many choose to use another character to terminate the ECHO statement, such as ...

echo/
echo(
echo;
echo:
etc.

There are many other characters that work. You can test them at a command prompt. The only requirement is that they must be immediately adjacent to the o in ECHO. I personally stick with the period and make sure to police my script repository to ensure it is free of an ECHO file. Others will argue the virtues of their personal favorite character for this purpose. I try to stay out of such discussions. ;^)
________________________
Tom Lavedas

Frank Westlake

unread,
Jan 21, 2013, 10:01:18 AM1/21/13
to
On 2013-01-21 05:31, Konrad Kullig wrote:
> What if you want to output only spaces?

I think you may be continuing the context of printing without a
linefeed. Here is a simple solution for that. Make the first character
something which may be deleted, make the second character a backspace,
then add one or more spaces and anything else you wish. To get a
backspace character you need either PowerShell or cScript.

A simple cScript solution is to save this file (setChar.cmd) in your
path:

@set @jScript=1;/*
@For /F "delims=" %%a in ('cScript /noLogo /E:jScript "%~f0" %~1') Do
@Set "ASCII-%~1=%%a"
@Goto :EOF
*/WScript.Echo(String.fromCharCode(WScript.Arguments(0)));

In your script

CALL setChar 8

and the backspace will be set into the variable "ASCII-8". Then to use
the backspace

SET /P "=x!ASCII-8! "<NUL:

If you would rather use PowerShell then this should be sufficient:

For /F %%a in ('PowerShell -NoProfile echo `b') Do Set "ASCII-8=%%a"


Frank


Konrad Kullig

unread,
Jan 21, 2013, 10:39:09 AM1/21/13
to
Thank you for the answers.

Using backspace is ok for console output but when I want to redirect the
output to a text file it is not pretty.

The only thing I see now is using a third party stream editor.
Either one that can replace <CR><LF> directly (is there such a common
program?) or combining both methods like this:
<nul set /p x=string1
<nul set /p "x=A "|sed -e s/A//
<nul set /p x=string2
will result to: string1 string2


"Konrad Kullig" <spam...@arcor.de> schrieb im Newsbeitrag
news:50fd4342$0$6554$9b4e...@newsspool4.arcor-online.net...

Frank Westlake

unread,
Jan 21, 2013, 11:00:52 AM1/21/13
to
On 2013-01-21 07:39, Konrad Kullig wrote:> Using backspace is ok for
console output but when I want to redirect the
> output to a text file it is not pretty.
>
> The only thing I see now is using a third party stream editor.
> Either one that can replace <CR><LF> directly (is there such a common
> program?)

If cScript was acceptable for creating a backspace then you can use
cScript to solve your problem by using either VBS or jScript to output
only the string you give it.

Frank

foxidrive

unread,
Jan 21, 2013, 11:34:16 AM1/21/13
to
On 22/01/2013 2:01 AM, Frank Westlake wrote:
> On 2013-01-21 05:31, Konrad Kullig wrote:
>> What if you want to output only spaces?

> Make the first character
> something which may be deleted, make the second character a backspace,
> then add one or more spaces and anything else you wish. To get a
> backspace character you need either PowerShell or cScript.

forfiles can give you a backspace in batch.

--
foxi

Tom Lavedas

unread,
Jan 21, 2013, 2:53:24 PM1/21/13
to
I completely missed the boot on the 'spaces' question, earlier. However, to expand on Frank's answer, here is a tiny hybid batch/vbscript procedure that effectively strips the newline characters off of any line of text that can be piped into it ...

:: WriteNNL.cmd, Tom Lavedas 01/21/2013
:: Syntax: echo.Some string/spaces | Write [>[>] text.ext]
::
@echo wsh.stdout.write wsh.stdin.readline> %temp%.\tmp.vbs
@cscript //nologo %temp%.\tmp.vbs & del %temp%.\tmp.vbs

So in this case, to output text to same line on the console, you could of something like this ...

echo. First part|writeNNL
:: some code ...
echo. |writeNNL
:: more code
echo Final part with newline
_____________________________
Tom Lavedas

frank.w...@gmail.com

unread,
Jan 21, 2013, 6:22:43 PM1/21/13
to
From Tom Lavedas :
>I completely missed the boot on the 'spaces' question,

Was that a "boat" you missed?

Frank

dbenham

unread,
Jan 21, 2013, 6:54:18 PM1/21/13
to
On Monday, January 21, 2013 8:31:42 AM UTC-5, Konrad Kullig wrote:
> What if you want to output only spaces?
>

The behavior of the SET /P prompt varies depending on the Windows version and also on how quotes are used. I've documented the full rules at http://www.dostips.com/forum/viewtopic.php?f=3&t=4209. Here is a rough summary:

- Leading spaces may be trimmed
- Leading quote requires additional quotes around the prompt
- SET /P will fail with a syntax error if the first non-trimmed character is =

The problems can be avoided when printing to the screen by leading with any character other than space, quote, or equal, followed by a backspace. But that has 2 limitations:

1) It will fail if the prompt is at last position in the line. The unwanted leading character will appear at the end, the cursor will scroll to the beginning of the next line, and the backspace cannot return to the prior line to erase the unwanted character.

2) If redirected to a file, then the output will include the unwanted character and the backspace.

jeb posted a very clever solution using a temporary file and Control-Z at http://www.dostips.com/forum/viewtopic.php?f=3&t=4213. His solution can output any text (except control-Z) without the carriage return / linefeed.

Here is a quick demo showing how it works. There is plenty of room for improvement of the routines. One example would be to direct all temp files to %TEMP%:

:: Begin Script :::::::::::::::::::::::::::
@echo off
setlocal

:: Define ctrlZ as ASCII(26)
copy nul ctrlZ.tmp /a >nul
for /f %%a in (ctrlZ.tmp) DO set "ctrlZ=%%a"
del ctrlZ.tmp

:: Test output to screen
call :screenWrite "=Hello"
call :screenWrite " world!"
echo(

:: Test output to a file
>test.txt (
call :write "=Hello"
call :write " world!"
)
type test.txt
echo(
del test.txt
exit /b

:screenWrite text
:: Only use this if writing to the screen (no redirection or pipe)
>txt.tmp (echo(%~1%ctrlZ%)
type txt.tmp
del txt.tmp
exit /b

:write text
:: This is safe in all cases
>txt.tmp (echo(%~1%ctrlZ%)
copy txt.tmp /a txt2.tmp /b >nul
type txt2.tmp
del txt.tmp txt2.tmp
exit /b
:: End Script :::::::::::::::::::::::::::::


Dave Benham

Zaidy036

unread,
Jan 21, 2013, 9:59:08 PM1/21/13
to
Why not keep it simple so that it is obvious to anyone reviewing the batch?

SET Line =This line
SET Line=%Line% has no CR-LF
SET Line=%Line% until the third line
ECHO %Line%

Todd Vargo

unread,
Jan 22, 2013, 12:30:33 AM1/22/13
to
That only appends text to the variable. It does not omit the CR-LF from
the ECHO command as the OP requested.

The trick above allows ECHOing text without a CR-LF. Note, the trick
does not SET the variable or alter it if it exists. The main benefit is
when the output is being sent to a file, and you want to add more text
to same line, typically from a source other than ECHO.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

dbenham

unread,
Jan 22, 2013, 6:32:14 AM1/22/13
to
On Tuesday, January 22, 2013 12:30:33 AM UTC-5, Todd Vargo wrote:
> On 1/21/2013 9:59 PM, Zaidy036 wrote:
> >
> > Why not keep it simple so that it is obvious to anyone reviewing the batch?
> >
> > SET Line =This line
> > SET Line=%Line% has no CR-LF
> > SET Line=%Line% until the third line
> > ECHO %Line%
>
> That only appends text to the variable. It does not omit the CR-LF from
> the ECHO command as the OP requested.
>

I see Zaidy036's point. Typically the desire to echo without CR/LF arises because you want to concatenate snippets of text from different points within a script into one line of output. The variable trick works in most cases, but it has some limitations:

- The line cannot grow larger than ~8191 bytes.

- There may be ENDLOCAL issues. I frequently toggle delayed expansion on and off within a FOR loop, and transporting the value of a variable across the ENDLOCAL barrier can be a pain, depending on the contents. Also, additional logic is often needed after a loop to check if the variable has been printed yet. It is much simpler to print the value without CR/LF the moment that I have it, rather than concatenating into a variable.


Dave Benham

Frank Westlake

unread,
Jan 22, 2013, 9:21:26 AM1/22/13
to
My apologies if this seemed offensive Tom, it was just another one of my
failed attempts at humor. I don't understand "boot" as it is used and
thought it might have been a typographic error of "boat".

Frank

Bob

unread,
Jan 22, 2013, 10:13:28 AM1/22/13
to
Frank, I thought Tom was being creative using the word 'boot' meaning
that he missed part of the subject of the thread from the start or 'boot
up'. Guess Tom will have to sort this out. Your comment did make me chuckle!
Later.



Tom Lavedas

unread,
Jan 22, 2013, 10:22:21 AM1/22/13
to
Thanks for trying to save me, Bob, but it was some kind of Freudian slip, I guess (as opposed to any reasonable chance it was a typo - O and A are no where near each other on an American keyboard). I was thinking "boat" and typed "boot" for whatever reason.

And Frank, I am somewhat dense at times, but it was clear this time that you were just directing a little ribbing in my direction. No offense taken ;^)
____________________________
Tom Lavedas

Frank Westlake

unread,
Jan 22, 2013, 10:25:17 AM1/22/13
to
On 2013-01-22 07:13, Bob wrote:
> On 1/21/2013 6:22 PM, frank.w...@gmail.com wrote:
>> From Tom Lavedas :
>> >I completely missed the boot on the 'spaces' question,
>>
>> Was that a "boat" you missed?
>
> I thought Tom was being creative using the word 'boot' meaning
> that he missed part of the subject of the thread from the start or 'boot
> up'.

That's what I thought at first. I also considered that he might be
spelling "boat" the way Canadians pronounce it, which is "boot".

Tom's reply came in as I type this. Thanks Tom.

Frank

Todd Vargo

unread,
Jan 22, 2013, 11:10:04 AM1/22/13
to
Another limitation when appending a variable is where CMD is reSTARTed
in separate processes.

BTW, while following the subject line, BigJoe responded to a dead post
from "1 juli 2009", however, you are mainly preaching to the choir here.
Thanks anyway.
0 new messages