How to check if a Variable is set?
Asked by WET:

So I have 2 variables in my script, one that is set when the user triggers the deployment manually and second when the deployment is triggered automatically. Therefore, one of the variables will not be set.

I've tried:

```
if [[ -z "${bamboo.ManualBuildTriggerReason.userName}" ]]; then
? ? ?DEPLOY_USER="${bamboo.DependencyTriggerReason.triggeringBuildResultKey}"
else
? ? ?DEPLOY_USER="${bamboo.ManualBuildTriggerReason.userName}"
fi
```

but I get a bad substitution error. In the case because the bamboo.ManualBuildTriggerReason.userName doesn't exists when the job deploys automatically.

Then I've tried something like

```
DEPLOY_USER=${bamboo.DependencyTriggerReason.triggeringBuildResultKey:-${bamboo.ManualBuildTriggerReason.userName}}
```

Still got the same error.

What is the best way to check if the variable is set or not?

LE: It's related to <https://community.atlassian.com/t5/Bamboo-questions/Check-if-variable-exists/qaq-p/246606>


Answers:
author: Mike Burnham
body: I found another way after reading this question, so adding it here. Might not have worked when first posted.

You can refer to Bamboo variables using an _ instead of the usual . style, and they exist as traditional shell variables that way. So if I want bamboo.badvariable, I can do this:

```
echo var=${bamboo_badvariable}
```

comments: author: Phill Pafford
body: thanks, this helped me solve my issue


author: Andrecarlossilva
body: I made a workaround here to check:

PARAMETER_NO_TRAIL_SPACE="$(echo -e "${bamboo.ManualBuildTriggerReason.userName}" \| sed -e 's/\[\[:space:\]\]\*$//')"

if \[\[ -z "$PARAMETER_NO_TRAIL_SPACE" \]\]; then


author: Alexander
body: An indicator of a referenced variable not being set is if your [build fails](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html):
> You can:
>
> * reference a global or context specific variable in a build plan or deployment project
> * reference a variable which references another one, deep recursion is allowed
>
> There are couple of limitations:
>
> * **referencing a variable which isn't defined is an error, whole build or deployment will fail if you reference such variable**
> * cycles are not allowed and are considered as build or deployment project error.

If you want to check if a variable is set to the value you'd expect, I'd just create a script task that printed a message with the variable and look for it in the logs while debugging.

```
echo "Hello ${bamboo.ManualBuildTriggerReason.userName}!"
```

comments: author: WET
body: This doesn't help me, let's say I have some variables in my gradle.properties and they are loaded into bamboo and they are stage specific.

I would want to check if the variable is defined like a simple bash command:

```
if [[ -v blabla ]];?
```

this checks that $blabla variable exists.

Is that so complex to do in Bamboo?


author: Gerd Aschbrenner
body: Only ASCII letters and digits and "_" are supported well on all linux systems and shell scripts as variable names. (regexp: `[a-zA-Z_][a-zA-Z_0-9]*` )

See the posix definition of a name for example: <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_235>

<br />

So the workaround is to assign the value to a variable name without dots by using a tool that supports the dots in the variable name like:

    bamboo_ManualBuildTriggerReason_userName=$(env | grep ^bamboo\\.ManualBuildTriggerReason\\.userName= | cut -d= -f2-)


author: Andrecarlossilva
body: I am having this same problem.