Shell script printing contents of variable containing output of a command removes newline characters [duplicate] Shell script printing contents of variable containing output of a command removes newline characters [duplicate] linux linux

Shell script printing contents of variable containing output of a command removes newline characters [duplicate]


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" | odx0x0000: 40 41 08 42 43 09 44 45 0A 46 47 0B 48 49 0C 4A   @A.BC.DE.FG.HI.J0x0010: 4B 0D 4C 4D 0A                                    K.LM.0x0015:$ echo  $x  | odx0x0000: 40 41 08 42 43 20 44 45 20 46 47 0B 48 49 0C 4A   @A.BC DE FG.HI.J0x0010: 4B 0D 4C 4D 0A                                    K.LM.0x0015:$