How to use Backslashes in Injected Variables
Purpose
When backslashes are used in Bamboo variables created using the Inject Variables Plugin, they may be omitted from the output, or cause issues with adjacent characters. This article will show how backslashes can be used in such variables.
Solution
As this Plugin reads the variables using Java libraries, certain characters need to be escaped
There are two groups of characters that need to be escaped:
- Letters that, when following a backslash, create an escape sequence
- Other special characters
Escape Sequences
Escape sequences require a total of 6 backslashes to properly escape them. This is because of the different layers of escaping that need to happen:
- When injected from the file into Bamboo itself; and
- In variable replacement.
These Escape sequences include (but may not be limited to):
\a
- Alert/Bell character\b
- Backspace\f
- Form feed\n
- Newline\t
- (Horizontal) Tab\u
- Unicode character marker\v
- Vertical tab\x
- Hexadecimal marker
Other special characters
Other special characters, including the backslash, only need one backslash to escape them, as follows: \\
.
Example
For example, I have a sample variable inject file that contains this variable:
test3=this\\is\\\\\\a\\\\\\test
Once the variable file is processed, after the first round of escaping, we see the following in the log:
command 21-Jun-2017 15:48:57 Substituting variable: ${bamboo.inject.test3} with this\is\\\a\\\test
When the Variable is used in a Script task, for example:
echo "${bamboo.inject.test3}"
The output, after the second and final escaping process, is:
build 21-Jun-2017 15:48:57 this\is\a\test