views:

501

answers:

2

I'm writing a shell script which will store the output of a command in a variable, process the output, and later echo the results. Here's what I've got:

stuff=$(diff -u pens tape)
# process the output
echo $stuff

The problem is, the output I get from running the script is this:

--- pens 2009-09-27 10:29:06.000000000 -0400 +++ tape 2009-09-18 16:45:08.000000000 -0400 @@ -1,4 +1,2 @@ -highlighter -marker -pencil -POSIX +masking +duct

Whereas I was expecting this:

--- pens 2009-09-27 10:29:06.000000000 -0400
+++ tape 2009-09-18 16:45:08.000000000 -0400
@@ -1,4 +1,2 @@
-highlighter
-marker
-pencil
-POSIX
+masking
+duct

It looks like the newline characters are being removed somehow. How do I get them to say in? I haven't been able to find anything in the documentation about this, and I'm a newbie. Any ideas? Thanks in advance!

-- Larry

+6  A: 

If you want to preserve the newlines, enclose the variable in double quotes:

echo "$stuff"

When you write it without the double quotes, the shell expands $stuff into a space-separated list of words (where 'words' are sequences of non-space characters, and the space characters are blanks and tabs and newlines; upon experimentation, it seems that form feeds, carriage returns and back-spaces are not counted as space).


Demonstrating interpretation of control characters as white space. ASCII 8 is backspace, 9 is tab, 10 is new line (LF), 11 is vertical tab, 12 is form feed, 13 is carriage return. The first command generates a sequence of characters separated by the various control characters. The second command echoes with the result with the original characters preserved - see the hex dump. The third command echoes the result with the shell splitting the words; you can see that the tab and newline were replaced by blank (0x20).

$ x=$(./ascii 64 65 8 66 67 9 68 69 10 70 71 11 72 73 12 74 75 13 76 77)
$ echo "$x" | odx
0x0000: 40 41 08 42 43 09 44 45 0A 46 47 0B 48 49 0C 4A   @A.BC.DE.FG.HI.J
0x0010: 4B 0D 4C 4D 0A                                    K.LM.
0x0015:
$ echo  $x  | odx
0x0000: 40 41 08 42 43 20 44 45 20 46 47 0B 48 49 0C 4A   @A.BC DE FG.HI.J
0x0010: 4B 0D 4C 4D 0A                                    K.LM.
0x0015:
$
Jonathan Leffler
cool! thank you!
A: 

Jonathan is right. The reason being the shell will otherwise remove the whitespace for you..

Behrang Dadsetan