How to use Backslashes in Injected Variables

Still need help?

The Atlassian Community is here for you.

Ask the community



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:

  1. Letters that, when following a backslash, create an escape sequence
  2. 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:

  1. When injected from the file into Bamboo itself; and
  2. 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



Last modified on Oct 7, 2019

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.