Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

In Bitbucket Pipelines, why doesn't '$?' get the expected exit code from the previously run command?

paulwatson Atlassian Team Jul 21, 2017

In a script I have:


  - set +e  # turn off error-trapping

  - failing-tests

  - echo $?

I expect an exit code of 1, but get 0. What's going on?

3 answers

2 accepted

3 votes
Answer accepted

Do it like this instead: 

- step:
  name: notify
  image: cfcommunity/slack-notification-resource
- |
set +e
echo $?
- some other stuff
- yet more stuff


3 votes
Answer accepted

Pipelines executes the printf command before every command in a script section of the bitbucket-pipelines.yml file in order to print those commands to the logs. This means, for example, that:


  - mvn clean build


actually behave like this:


  - printf "+ mvn clean build\n"

  - mvn clean build


One consequence of this is that an attempt to use '$?' to pass the exit code of a failing command to the next command (when error-trapping is turned off) will fail because the next command actually receives the exit code of the printf command.

For example:


  - set +e  # turn off error-trapping

  - failing-tests

  - echo $?


will behave like this:


  - printf "set +e\n"

  - set +e  # turn off error-trapping

  - printf "failing-tests\n"

  - failing-tests

  - printf "echo $?\n"

  - echo $?  # with exit code of 0, because the preceding printf command succeeded


If you want to catch the exit code of a failing command, a workaround is to combine commands on the same line (to prevent the printf command from intercepting the exit code), like this:


  - set +e

  - failing-tests; echo $? # with exit code of 1 in this case 

You may also use `BITBUCKET_EXIT_CODE`

I am using: `set +e` to prevent `failing tests` from stopping Pipelines, so I can add some debugging commands, printout logs after the failing tests command.

However I would still like Pipeline to fail in case of failing test. With: `set +e` Pipeline passes and last command in my Pipeline

How can I make Bitbucket Pipeline fail or not, given the result of my tests?

I have the same issue.

I figured it out finally as follows:

set +e # turn off error-trapping

# do something here that might throw
git merge-base --is-ancestor $(git rev-parse master) HEAD
RETURN_CODE=$?  ## capture the exit code

if [ $RETURN_CODE -eq 1 ]; then
  echo "The current branch is not sibling of master"
  exit 1
  echo "The current branch is sibling of master"

set -e # turn on error-trapping

Like Paul Rohorzka likes this

Thanks @Mina Luke for sharing, I am using exactly the same approach (Forgot to share it here though) for  and it works flawlessly.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,927 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you