how to avoid \n being converted to space when echo a variable in bash script? [closed]

StackOverflow https://stackoverflow.com/questions/21629268

  •  08-10-2022
  •  | 
  •  

سؤال

I have one file a with content:

hola
hallo
hello

I wrote this script to read its content:

#!/bin/bash
l=`cat a`
echo $l | hexdump -C #this line is only for debugging
#another program will use the content of $l after I write $l to a fifo
echo $l > my_fifo &
#program reads my_fifo
......

its content was changed after the first echo:

00000000  68 6f 6c 61 20 68 61 6c  6c 6f 20 68 65 6c 6c 6f  |hola hallo hello|
00000010  0a                                                |.|

actually it should be:

$ cat a | hexdump -C
00000000  68 6f 6c 61 0a 68 61 6c  6c 6f 0a 68 65 6c 6c 6f  |hola.hallo.hello|
00000010  0a                                                |.|

I dont think using "sed 's/ /\n/g'" to replace all spaces is a good idea, because my input-file is actually not so simple. how to keep it unchanged after echo?

هل كانت مفيدة؟

المحلول

Please use double quotes to avoiding replaces of the `\r to when using evaluation or assignments, or operations::

echo "$l" | hexdump -C
00000000  68 6f 6c 61 0a 68 61 6c  6c 6f 0a 68 65 6c 6c 6f  |hola.hallo.hello|
00000010  0a            
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top