Script Question (39 Views)
Regular Advisor
Posts: 152
Registered: ‎08-01-2001
Message 1 of 5 (39 Views)

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
Posts: 13,908
Registered: ‎06-21-2000
Message 2 of 5 (39 Views)

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
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (39 Views)

Re: Script Question

Hi Jason:

# echo "\\\n"


Honored Contributor
Posts: 1,461
Registered: ‎03-10-1998
Message 4 of 5 (39 Views)

Re: Script Question

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


PS : single quote !
fiat lux
Honored Contributor
Posts: 764
Registered: ‎08-23-2002
Message 5 of 5 (39 Views)

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"
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.