Solved multiple echoes to same line

Is it possible to use multiple echoes and output to same line?

ie so that

echo 'abc ' > myline
echo 'def ' >> myline
echo 'xyz' >> myline

create a 'myline' consisting of 'abc def xyz'

rather than

abc
def
xyz
 
A line is a possibly empty sequence of non‑newline characters followed by exactly one newline character. echo always emits one newline character after the given arguments (although there are implementations that support a ‑n switch disabling that). If you do not want that use printf
Bash:
{
	printf 'abc '
	printf 'def '
	echo   'xyz'
} > myline
In fact POSIX™ recommends using printf instead of echo for portability reasons, so maybe get used to it.​
 
echo is often a shell built-in so depending on shell behavior may be different.
But most of the common shells, "echo" has a "-n" option which means "Do not print the trailing newline character"
So
echo -n "abc" > myline
echo -n "def" >> myline
echo "xyz" >> myline
should give you
abcdefxyz followed by a newline
 
Threads like this are gold. Simple question, way too many answers. old saying about "different ways to skin a cat".
Keep them in your pocket because someday, you will look like Einstein when you offer them.
 
Back
Top