Access Bamboo variables in bash script
Asked by Hardik Kamdar:

I want to access the global variables set in bamboo in a bash script that I am running on my local. How can I access those variable?


Answers:
author: jvelapol
body: The following works for me in a "script" task:

```
#!/bin/bash
bash /path/to/some/shell/script/on/bamboo/server/script.sh
```

where /path/to/some/shell/script/on/bamboo/server/script.sh looks something like:

```
#!/bin/bash
# Note underscore in definition
echo "worldName was set to: ${bamboo_worldName}"
exit 0
```

And in the "Variables" tab of a plan, I set worldName to "foo". When bamboo runs, it outputs:

worldName was set to: foo

Note that in the script task itself, it correctly reads and interprets a bamboo.variable, but when you run in a sub-shell (say, calling/running an external shell script from the "script" task), you just have to translate it to bamboo_variable.

Also, sorry for the *very late* reponse.

EDIT: I only now read your last comment, which was basically, "can you modify the global variables set in Bamboo by running a script locally inside Bamboo?" The answer is probably "no" - a cursory scan through the REST API showed that you can change them on a per-instance case (if they're a queued plan), or trigger a build/deploy via the REST API overriding existing variables, however, I didn't see a way to actually set the "Global Variables" in Bamboo overall directly. Note that with *some* things, I've had to scrape HTML to figure out what's going on. Essentially, create a Selenium "test" that automatically clicks through various buttons and whatnot to post the relevant information. It's a *terrible* hack and highly fragile, and likely to break something if you're not careful.

comments: author: Dineshmathiazhagan
body: In inline script you may have used bamboo.variable similarly use bamboo_variable.


author: jvelapol
body: True - I think the difference is that using \`bamboo.variable\` means that the Bamboo Java Process does a bulk string find/replace _before_ it creates the shell script that then gets executed. We've shied away from using that, as we've mostly stopped doing "things" in the "Script" task - instead adding them to a shell script in the code repo, and executing from there.

In that case, \`bamboo.variable\` is not a valid env variable in shell or bash, so those fail.


author: Marcin Oles
body: Hi Hardik,

to do so use this syntax:

```
${bamboo.<variableName>}
```

for example

```
#!/bin/shecho "Hello, ${bamboo.worldName}!"exit ${bamboo.exitCode}
```

Such variables will not get evaluated by shell, because prior to being executed Bamboo will replace all identified variables with their values.

More detailed guide can be found here: <https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html>

---------- UPDATE ----------

As I mentioned in the comment, substituting variables will not work for script tasks run from a file. Only inline scripts will be substituted.

A workaround for this would be to use variables during configuring the Script task, as script arguments or environment variables. See the screenshot for more info:

script-task-variables.png

Cheers,  
Marcin  
[](https://community.atlassian.com/t5/Bamboo-questions/Access-Bamboo-variables-in-bash-script/qaq-p/478758?attachment-id=15084){#link_1171d4d2eac1c9}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/17404/1/script-task-variables.png){#display_1171d4d2eac1c9_0}

comments: author: Hardik Kamdar
body: Hello Marcin,I have created a global variable for demo purpose. The variable is called demovariableI created a bash script as follows:#!/bin/bashecho ${bamboo.demovariable}I am getting the error '${bamboo.demovariable}: bad substitution'Am I doing anything wrong?P.S. Just to divert your attention to this important fact, I am trying to access these variables in a bash script running on my local machine and not as Bamboo inline script


author: Marcin Oles
body: "bad substitution" is an error message from bash. The variables are not being substituted, so they are left in the script which then fails to execute (bash and sh can't handle the dot in variable names).I've verified that variables will not be substituted in script files, only for inline scripts. That's probably the reason.As a workaround, you can use variables in "script arguments" and "environment variables" in the task configuration, and then use the arguments or env vars in the script.Cheers,Marcin


author: Hardik Kamdar
body: Hello Marcin,I did get the point wherein you mentioned why cannot the bash access the variable using the dot.But I am unable to understand the workaround that you have mentioned. Can you please explain that in greater detail?


author: Marcin Oles
body: Updated my answer with more details and a screenshot of an example.


author: Hardik Kamdar
body: Hello Marcin,Thanks for the prompt reply. I think we both are probably proceeding on different tasksI shall explain the exact requirement:I have bamboo configured on my machine. I have set some predefined global variables in Bamboo. I have a bash script running on my local machine performing some operations. Some results are being generated. I want to now updates these results into the global variables that are present in my bamboo automatically from my local bash script itself. Can you tell me how should I be doing that?Regards,Hardik


author: Kira Resari
body: I have the same issue.

Specifically, I have a Variable:

\* Variable name: JAVA_11_PATH

\* Value: /usr/lib/jvm/java-11-amazon-corretto

And a script that I want to use like this:

\`\`\`

./gradlew clean build -Dorg.gradle.java.home=JAVA_11_PATH

\`\`\`

But that returns this error:

\`\`\`

Value 'JAVA_11_PATH' given for org.gradle.java.home Gradle property is invalid (Java home supplied is invalid)

\`\`\`

If I manually substitute \`JAVA_11_PATH\` for the value in the script, it works. So obviously it's just the variable substitution that fails.

So, how can I make this work?

comments: author: jvelapol
body: If you're using the "Script" task in Bamboo, you have two options:

1.

   ```
   ${bamboo.JAVA_11_PATH}
   ```

2.

   ```
   ${bamboo_JAVA_11_PATH}
   ```

I *strongly* recommend going with option 2. Generally, I've found the "Script" task to be not so hot, other than to prototype. We instead tend to copy/paste the contents of the "Script" task into an actual script in the codebase instead (for example a \`build.sh\` script in the codebase in, like \`bin/build.sh\`). And then the "script" task just looks something like:  

```
#!/bin/bash

BUILD_SCRIPT=bin/build.sh
if [ ! -r ${BUILD_SCRIPT} ] ; then
? echo "ERROR: could not find ${BUILD_SCRIPT}" >&2
fi

if ! ${BUILD_SCRIPT} ; then
? echo "ERROR:? executing ${BUILD_SCRIPT} failed" >&2
? # <do whatever relevant cleanup you need to do>
fi
```

Managing that script in your code repo also means that it's far simpler to manage the actual build than to try to handle changes that happen in the Script task in Bamboo directly. Note that the "config as code" has made it somewhat easier to manage, but even that is still pretty clunky.


Tags: bamboo-administration

bamboo-development