Operating System - Linux
1752777 Members
6102 Online
108789 Solutions
New Discussion юеВ

echo with NO new line option

 
SOLVED
Go to solution
Emanuele_4
Regular Advisor

echo with NO new line option

Hi everybody!

I have an apparently simple question...but I don't resolve it anyway!

I want to make a simple script:

#!/sbin/sh
touch file.txt
echo -n "first line" >> file.txt
echo "second line" >> file.txt

this script write a file with 2 TWO lines...but I want to write a single line with two different echo command.

I know that echo -n doesn't write the new line at the end of the command but...it does!!
So...the -n option doesn't work...and even the \n option...so how can I write a single line?

Thanks in advance to anybody!

Emanuele
7 REPLIES 7
Bill Hassell
Honored Contributor
Solution

Re: echo with NO new line option

Change the code from:

#!/sbin/sh
touch file.txt
echo -n "first line" >> file.txt
echo "second line" >> file.txt

to:

#!/sbin/sh
touch file.txt
echo "first line\c" >> file.txt
echo "second line" >> file.txt


Bill Hassell, sysadmin
Nitin Kumar Gupta
Trusted Contributor

Re: echo with NO new line option

Hi

Hassel suggested very correct. Actually it is correct that you are using -n option, but this option is not working with all the unix environment.

Also if you want to feed extra line, you can use \n option.
Like

echo "Test \n"

Regards
Nitin
Nitin Kumar Gupta
Trusted Contributor

Re: echo with NO new line option

Hi

Hassel suggested very correctly. Actually it is correct that you are using -n option, but this option is not working with all the unix environment.

Also if you want to feed extra line, you can use \n option.
Like

echo "Test \n"

Regards
Nitin
Peter Nikitka
Honored Contributor

Re: echo with NO new line option

Hi,

best is NOT to use 'echo' but the posix shell builting 'print' - this will work across all platforms:

print -n string1 string2

will NOT append a newline.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Steven Schweda
Honored Contributor

Re: echo with NO new line option

Pre-Posix, one way to handle this problem
was to test "echo" to see how it works:

echo -n x | grep n > /dev/null

If "-n" works, the status is non-zero. If
"-n" does not work, it's zero.

td176> uname -a
HP-UX td176 B.11.23 U ia64 1928826293 unlimited-user license

td176> echo -n x
-n x
td176>

td176> echo -n x | grep n > /dev/null ; echo $?
0

urtx# sizer -v
Compaq Tru64 UNIX V5.1B (Rev. 2650); Thu Nov 23 02:10:08 CST 2006

urtx# echo -n x
xurtx#

urtx# echo -n x | grep n > /dev/null ; echo $?
1

When your script knows what works, it can
define an alias or a function to do whatever
works. (Years ago, I named mine "echn".) As
usual, there are many ways to solve a problem
like this.
Dennis Handly
Acclaimed Contributor

Re: echo with NO new line option

As mentioned by everyone else, you are dependent on the scummy C shell. If you look at echo(1) it says that -n is only supported by the csh builtin and the Berkeley semantics.

A real POSIX shell or echo doesn't support -n.
Emanuele_4
Regular Advisor

Re: echo with NO new line option

proble resolved with the \c option.

I was wrong because I put the \c out of "".... :-)

Thanks to everybody

Emanuele