Showing results for 
Search instead for 
Do you mean 

Script Question

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?
Honored Contributor [Founder]

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.
Acclaimed Contributor [Founder]

Re: Script Question

Hi Jason:

# echo "\\\n"


Honored Contributor [Founder]

Re: Script Question

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


PS : single quote !
fiat lux
Honored Contributor [Founder]

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"