0

I want to create a variable that contains a string of text [in BASH] that spans multiple lines and includes spaces. I can do this without spaces, but not with spaces.

I am trying to create a flash card program for self study. I am not in school and this is not homework.

This works:

$ TEST=me$'\n'you
$ echo "$TEST"
me
you

This does not work:

$ TEST="me on line one$'\n'you on line two"
$ echo "$TEST"
me on line one$'\n'you on line two

I would like the output of the second code to look like this:

me on line one
you on line two

Note: this is not a duplicate question. The alleged duplicate does not contain an example with spaces, which is why I had a problem.

JustinMT
  • 13
  • 3
  • https://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash – LMC Apr 17 '19 at 04:08
  • Neither of those questions are duplicates to mine. Those questions do not contain spaces in the strings, which is why I had a problem. – JustinMT Apr 17 '19 at 18:13

1 Answers1

1
$ test=$'me on line one\nyou on line two'
$ echo "$test"
me on line one
you on line two

If you insist on using different quoting methods for different parts of the string (why?), you need something like

$ test="me on line one"$'\n'"you on line two"
melpomene
  • 84,125
  • 8
  • 85
  • 148