How can a heredoc string be output as it is?

When I print out this heredoc string, it is output as a single line. How can it be configured to print out on multiple lines just as it is in the heredoc?

sslCmd = """
openssl req -x509 -new -nodes -sha256 \
 -key {{key-file-pass}}.key \
 -days 3650 \
 -out {{key-file-pass}}.pem \
 -subj "\
/C={{ca_cert_subj_country}}\
/ST={{ca_cert_subj_state}}\
/L={{ca_cert_subj_location}}\
/O={{ca_cert_subj_organization}}\
/OU={{ca_cert_subj_org_unit}}\
/CN={{ca_cert_subj_common_name}}\
/emailAddress={{ca_cert_email}}"\
"""

IO.puts sslCmd

output

openssl req -x509 -new -nodes -sha256  -key {{key-file-pass}}.key  -days 3650  -out {{key-file-pass}}.pem  -subj "/C={{ca_cert_subj_country}}/ST={{ca_cert_subj_state}}/L={{ca_cert_subj_location}}/O={{ca_cert_subj_organization}}/OU={{ca_cert_subj_org_unit}}/CN={{ca_cert_subj_common_name}}/emailAddress={{ca_cert_email}}"

PS. I expect the output below to appear on a single line with a horizontal scrollbar to bring the rest into view when I used the preformatted text toolbar button. Why is it wrapped?
Thanks!

Hmm it seems to work on my iex.

Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> cmd = """
...(1)> abc
...(1)> sdlekj
...(1)> coiemc
...(1)> """
"abc\nsdlekj\ncoiemc\n"
iex(2)> IO.puts cmd
abc
sdlekj
coiemc

:ok
iex(3)>

Is it OS issue? \n might behave weirdly on Windows?

1 Like

You have added the escape character \ to the end of each line which removes the line break in the heredoc.

"""
a\
b\
c\
"""
#=> "abc"

"""
a
b
c
""""
#=> "a\nb\nc\n"
3 Likes

You are right. It works when I use \\ instead.

The output is supposed to go into a bash script and maintained that way for readability.

1 Like

You can also use sigils in order to avoid having to escape special characters. You can read more about sigils in the Getting Started guide. Here is an example:

~S"""
a\
b\
c\
"""
#=> "a\\\nb\\\nc\\\n"
3 Likes