-1

Hi I have a shell script that has

variable="apple banana monkey"

I want it to be

apple\nbanana\nmonkey

But when I try and execute

echo $variable | tr ' ' '\n'

It results to

apple
banana
monkey

I want to get the actual literal of new line and not the evaluated value.

I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.

Please help. Thanks

Paolo Lambojon
  • 155
  • 1
  • 1
  • 11
  • Does this answer your question? [Replacing newline escape '\n' with an escaped newline escape '\\n' using sed](https://stackoverflow.com/questions/30785650/replacing-newline-escape-n-with-an-escaped-newline-escape-n-using-sed) – Mickael B. Jan 24 '20 at 09:27
  • @MickaelB. We tried this already. – Paolo Lambojon Jan 24 '20 at 09:36

1 Answers1

0

tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.

If you accept a command switch, you can try with sed:

echo "$variable" | sed  's/ /\\n/g'
Poshi
  • 5,332
  • 3
  • 15
  • 32