Showing results for 
Search instead for 
Did you mean: 

Script Question

Jason Berendsen
Regular Advisor

Script Question

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

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?
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"


Jean-Luc Oudart
Honored Contributor

Re: Script Question

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


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"