1748232 Members
3645 Online
108759 Solutions
New Discussion юеВ

Script Question

 
Jason Berendsen
Regular Advisor

Script Question

I need to have a script newline characters into an output file.

Example:
echo "This is a test\n\n" > /tmp/testing

Of course this resulted in the file having two blank lines after the sentence. I tried to use \\n\\n to no avail. I attempted to use sed, again to no avail.

How can I echo \n into a file and have it mean \n and not newline?
4 REPLIES 4
Patrick Wallek
Honored Contributor

Re: Script Question

A very basic way:

echo "This is a test" > /tmp/testing
echo "" >> /tmp/testing
echo "" >> /tmp/testing


The above will "This is a test and then append 2 blank lines to the file.
James R. Ferguson
Acclaimed Contributor

Re: Script Question

Hi Jason:

# echo "\\\n"

Regards!

...JRF...
Jean-Luc Oudart
Honored Contributor

Re: Script Question

echo 'This is a test\\n\\n' > /tmp/testing

Rgds,
Jean-Luc

PS : single quote !
fiat lux
curt larson_1
Honored Contributor

Re: Script Question

you really should be using print instead of echo. in which case

\\ you need to type two backslashes for a backslash

print test\\\\n

caution \ is a quote character and the shell will remove it when it expands the command, unless the \ is quoted

print 'test\\n'

and the easiest way would be to use print with the -r switch. -r, the shell does not use the \ conventions.

print -r "test\n"