0

I created the following file called birthday, which contains the following strings:

Happy Birthday
Happy Birthday
Happy Birthday

when I use the command cat birthday the shell will output the text string with the line spacing

Happy Birthday
Happy Birthday
Happy Birthday

when I try to declare a variable like this - and echo it back everything is truncated on a single string. How do I keep the spacing and pass it to a variable:

birthday=$(cat birthday)  && echo $birthday

Output:

happy birthday happy birthday happy birthday
Ian Arman
  • 93
  • 3
  • 9

1 Answers1

0

Without quotes, the shell expands the variable and performs word splitting. Thus, it sends 6 parameters to echo, which prints them all separated by spaces.

Use double quotes to prevent word splitting.

echo "$birthday"

BTW, "truncating" usually describes shortening data by removing bytes at the end, not at random positions inside the data.

choroba
  • 231,213
  • 25
  • 204
  • 289