Click to See Complete Forum and Search --> : String with newlines in shell script (ksh)


dchidelf
09-29-2001, 03:11 PM
Is there a way to create a string that includes newlines in a shell script? (ksh)

This sounds easy enough, but I only want the definition to take one line in the script.

Background:
I'm using ex to edit a file from a shell script. I can do this:

ex -s -c "5i
This text is inserted on line 5
.
wq" filename

but I'd rather put it all on one line.
This resulted in my next step

N="
"
ex -s -c "5i${N}This text is inserted on line 5${N}.${N}wq" filename

The definition of N really bothers me though.
finally I resorted to

echo "5i\nThis text is inserted on line 5\n.\nwq" | ex -s -c - filename

I rely on echo to evaluate the escaped newlines and pass it to ex as the parameter for -c.
This works...but do I have to pipe it?

From the command line I can do something like

ex -s -c "5i^JBlah Blah Blah^J.^Jwq" filename

but in a script the ^J is actually a newline, so it's not really one line and I'm back were I started.

Using ticks (`cmd`) changes the newlines into spaces or something, so

ex -s -c `echo "5i\nblah\n.\nwq"` filename

doesn't work

Is there a way?

EscapeCharacter
09-29-2001, 04:30 PM
echo requires the -e switch to enable support to for escape sequences

dchidelf
09-29-2001, 05:47 PM
On the HP-UX system I work on, echo accepts only one switch (-n to turn off the trailing newline). It processes escape characters without the -e switch.