travis-build: preserving the status in $? #3771

grooverdan opened this Issue Apr 27, 2015 · 8 comments


None yet

5 participants


Whether make succeeds or fails $? is always 1.

  - make -j 4


The command "make -j 4" exited with 0.

The command "COMPILE_RESULT=$?" exited with 0.


Each line in the script section is processed by the travis_cmd bash function. (The value 1 comes from the immediately preceding test [[ -n "$retry" ]] on line 38.)

I understand the motivation for using $? this way (and potentially other special variables) in the script section, but achieving it without breaking the currently working builds may be hard.

I tend to think it is better to document this behavior, and advise writing a custom script if the user wants to do something involving $? in the script section.


I think anyone who's tried to use $? has come across this limitation and avoided it by writing a custom script (like I should be doing now). Thanks for the prompt response.

@BanzaiMan BanzaiMan added a commit to travis-ci/docs-travis-ci-com that referenced this issue Apr 27, 2015
@BanzaiMan BanzaiMan Add note on $? based on discussion in travis-ci/travis-ci#3771 a5a414b

I've noted this in (Improvement suggestions welcome!)

I'm closing this now.

@BanzaiMan BanzaiMan closed this Apr 27, 2015

The trick is, you have to get it right after the command.

make -j4; export COMPILE_RESULT=$?

Though in my .travis.yml I usually just combine the command using "&&" or "||" bash operator or inside another if statement, so I don't really need to use $? explicitly to get the exit status of the earlier command. e.g.:

make -j4 && other-cmd-to-execute-when-make-was-successful


make -j4 || other-cmd-to-execute-when-make-was-failed

@BanzaiMan , while I'm still not convinced that there is a person using $? to extract the value of [[ -n "$retry" ]] the alternative for documentation is here:

- [ $COMPILE_RESULT -ge 2 ] && echo makefile error

So the two objectives in deciding this structure where to make $COMPILE_RESULT have the value of the return and to give travis a true/false indicator of the result of the compile. @weitjong suggestions are of course valid (though export is excessive) however sometime additional flexibility is needed.


Improvement to the documentation is also welcome!

@ctubio ctubio referenced this issue in ctubio/ May 4, 2015

Fix false positive builds in travis. #12

@l2fprod l2fprod added a commit to IBM-Bluemix/insights-search that referenced this issue Oct 7, 2015
@l2fprod l2fprod use one liner (travis-ci/travis-ci#3771) bc6f6cc
@Artoria2e5 Artoria2e5 referenced this issue in travis-ci/travis-build Oct 15, 2015

travis_buildtempl_header: Save full PIPESTATUS #528

@ryansmith94 ryansmith94 added a commit to LearningLocker/learninglocker that referenced this issue Feb 23, 2016
@ryansmith94 ryansmith94 Tries using ||. b3f0c34

Thanks @weitjong 👍


Does Travis capture the non zero exit code for init scripts like sudo service xyz start ? In my case though the service fails to start it shows build successful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment