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

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?
Honored Contributor
Patrick Wallek
Posts: 13,787
Registered: ‎06-21-2000
Message 2 of 5 (37 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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (37 Views)

Re: Script Question

Hi Jason:

# echo "\\\n"

Regards!

...JRF...
Honored Contributor
Jean-Luc Oudart
Posts: 1,461
Registered: ‎03-10-1998
Message 4 of 5 (37 Views)

Re: Script Question

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

Rgds,
Jean-Luc

PS : single quote !
fiat lux
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 5 of 5 (37 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.