Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!mtune!whuts!homxb!mhuxt!m10ux!mnc From: mnc@m10ux.UUCP (Michael Condict) Newsgroups: comp.unix.wizards Subject: Re: embedded newlines in shell variable? Message-ID: <323@m10ux.UUCP> Date: Thu, 13-Aug-87 15:26:01 EDT Article-I.D.: m10ux.323 Posted: Thu Aug 13 15:26:01 1987 Date-Received: Sat, 15-Aug-87 14:28:25 EDT References: <228@cjsa.UUCP> Organization: AT&T Bell Labs, Murray Hill Lines: 47 Keywords: sed sh shell Summary: How to get new lines in sed command In article <228@cjsa.UUCP>, jeff@cjsa.UUCP writes: > I have been attempting to construct a 'sed' command line script and load it > into a shell variable (called ACTION) for later use within a bourne shell > script. All goes well until I need sed to append new text to the end of the > input data. To work, sed's append command ( a\ ) requires newlines to be > embedded as follows: > '$a\ > NEW LINE OF MATERIAL' > > . . . The following dialog worked for me, using /bin/sh. Note that I use explicit new lines, rather than the "\n" notation. Also note that you should not escape a "$" character that is inside single quotes -- the escape is not interpreted specially there and will leave a literal escape character in your string, which sed will barf on: $ cmd='$a\ > dog' $ echo 'hello > goodbye' | sed -e "$cmd" The sed command appends "dog" after the last line of input, which in this case produces: hello goodbye dog as output. This is on System V Rel. 2, by the way. I think your problem is that you are mistaken about the meaning of "\n" in shell strings. It is not transformed on input to a newline character. The echo command will transform it to a new line on output, but other commands may or may not process it as a new line. To prove this to yourself, try: $ str='a\nb' $ cc "$str".c Cc will complain that it cannot open the file "a\nb.c", thus indicating that the "\n" sequence was inserted unchanged into the string and was left unchanged by the shell when it fed the string to cc. -- Michael Condict {ihnp4|vax135|cuae2}!m10ux!mnc AT&T Bell Labs (201)582-5911 MH 3B-416 Murray Hill, NJ