johnnycrash
johnnycrash

Reputation: 5344

bash cat removes characters

I am trying to read an entire file into a variable without removing any characters. I'm sure this has to be stupid simple.

This doesn't work, since it removes repeating spaces, all tabs and newlines:

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

I can see the same filtering happening with simple assignment like this:

$ 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 upon assignment?

Upvotes: 4

Views: 386

Answers (2)

Brian Roach
Brian Roach

Reputation: 76908

Figured it out:

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

Upvotes: 2

pajton
pajton

Reputation: 16226

Use echo "$var":

$ z='fred                 wilma'
$ echo "$z"
fred                 wilma

Upvotes: 5

Related Questions