Bash cat removes characters

I am trying to read the entire file in a variable without deleting any characters. I'm sure it should be stupidly simple.

This does not work as it removes duplicate spaces, all tabs and newlines:

$ echo 'fred wilma' > somefile; z=$(cat somefile); echo $z fred wilma 

I see that the same filtering happens with a simple purpose:

 $ z='fred wilma'; echo $z fred wilma 

but not when I do this:

 $ echo 'fred wilma' fred wilma 

How do I get a bash variable to stop being parsed and filtered on assignment?

+4
source share
2 answers

Use echo "$var" :

 $ z='fred wilma' $ echo "$z" fred wilma 
+5
source

It revealed:

 echo 'fred wilma' > somefile; z=$(cat somefile); echo "$z" 
+2
source

All Articles