Use a variable exported in a custom script in Bitbucket Pipelines

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

Let's say the user has an external script to do some deployment work. The user calls the script in the script section of the pipeline as below:

1 2 3 4 5 pipelines: default: - step: script: - ./script.sh

In the script, they want to export some variables to use in other commands within the same step in Bitbucket Pipelines.

Here is an example of the script content:

1 2 3 #!/bin/bash export TEST_VARIABLE='Test variable.'

Environment

Bitbucket Pipelines

Diagnosis

In this scenario, if the user tries to use $TEST_VARIABLE outside the script it won't work. Example:

1 2 3 4 5 6 pipelines: default: - step: script: - ./ script.sh - echo $TEST_VARIABLE

Cause

To have those variables available, the script must be executed in the context of the calling shell.

Solution

Do not call the script with the "./" syntax, use one of the following options instead:

1 . script.sh

or

1 source script.sh

Example of a working Pipeline for this scenario:

1 2 3 4 5 6 pipelines: default: - step: script: - source script.sh - echo $TEST_VARIABLE

If the above instructions do not assist in achieving the desired outcome, please raise a support ticket or raise a community support ticket for further assistance with this.

Updated on March 24, 2025

Still need help?

The Atlassian Community is here for you.