Mystery solved. There’s no bug anywhere, this is how newline char interpretation works in Bash + Slack API.
So, what you have to know: \
+ n
is not the same as a newline character, usually represented by the \n
escape sequence.
\
+ n
are two characters. In a string you can include \
+ n
, and in fact if you run the Bash script that’s what happens. It simply means \
and then a n
char.
A newline character is a single character, which is usually represented by \n
as an escape sequence, but the newline character is actually the ASCII code 10 char (see ASCII - Wikipedia )
Bash does not treat \n
as a newline char (as a single char), but most programming languages do when you specify \n
in a String variable value (e.g. Go), that’s why \n
worked in the Go script.
Don’t believe me? Try this script; simply save it into test.sh
and then run bash test.sh
to run it:
#!/bin/bash
set -ex
multiline_str="Multi\nline\n\nstring"
echo "$multiline_str"
You’ll get the exact same output as if you run this as a Script step in Bitrise config:
$ bash test.sh
+ multiline_str='Multi\nline\n\nstring'
+ echo 'Multi\nline\n\nstring'
Multi\nline\n\nstring
While if you run this:
#!/bin/bash
set -ex
multiline_str="Multi
line
string"
echo "$multiline_str"
you get:
+ multiline_str='Multi
line
string'
+ echo 'Multi
line
string'
Multi
line
string
So, what’s the issue?
The thing what causes the confusion is that the Slack API docs mentions
You can post multiline messages through Slack’s APIs. Insert a newline by including the characters \n in your text. For example:
But they mean the \n
escape sequence there, not \
+ n
as characters. As I said, whether \
+ n
are treated as two separate characters or as a single escape char for ASCII 10 depends on where you define it.
In Bash \n
are two chars, while in Go for example that’s a single character.
This means that the solution I mentioned before with
is not a workaround, that’s exactly how the \n
newline char can be represented in Bash when calling the Slack API.
If you’d have any questions @toshi0383 just let us know!
Edit: Slack step v2.5.0 now auto-converts the char sequence to the escape character, see Slack step does not treat "\n" as a newline when message generated by a Bash script step - #9 by viktorbenei and Slack step does not treat "\n" as a newline when message generated by a Bash script step - #10 by viktorbenei