When is the use of “||” needed in bash?

I use Travis CI to do unit testing for my open source project. While configuring my .travis.yml file I’ve seen examples on several other projects.

When the statement is executed as a conditional, I understand the need for || true to ensure the command returns “success” and allows the script to continue. A non-match on the name would be false, omit execution of later commands after && , but eventually return successful after || true .

- '[ "${TRAVIS_OS_NAME}" = "osx" ] && brew install ant || true'

However, I have also seen the conditional applied after if ... fi conditionals, such as this example:

- if [ "${TRAVIS_CPU_ARCH}" == "arm64" ]; then 
    sudo apt-get install openjdk-11-jdk libltdl-dev;
    export JAVA_HOME=/usr/lib/jvm/SolarUltima/java-11-openjdk-arm64;
    export PATH=$JAVA_HOME:$PATH; 
  fi || true

My own testing indicates it makes no difference whether that’s included or not. For example, this conditional does not stop the test, even when it evaluates to false. Like below:

if [ "${TRAVIS_BRANCH}" == "coverity_scan" ]; then exit 0; fi

Travis produces this output, showing a “true” result, so for me it begs the question when is the proper time to use || in bash.

Hey Ultima,

This answer is a little nebulous, but I’ll do my best. When the script is executed in an environment variable when its execution will stop if any of the expressions will return with nonzero exit status. Such examples are with set -e or say in Python, an example would be *args and or **kwargs. This is true with mostly all CI/CD tools.

The hierarchy would look like this:

false && true
#  $?Montana is 1

This will ultimately exit with a nonzero exit status. I find people, including myself using the || true iterator, or tersed, ||: in such scripts to make the commands lists with && (on some exclusions grep can be in there). This will exit with zero status even if one of the commands fail, this can be avoided occasionally by using the skip flag. Typically:

 [ -e "env_file" ] && . "env_file" ||:

I have also seen the conditional applied after if, so something like fi conditionals. You’ll see similar things in bash scripts with more complex Docker instructions. I do this when setting more complex Docker environments up.

Really in this niche case when concerning Travis CI, this certain usage you showed seems to be some left-over or inserted conditions by some SecOps programmer I’d have to assume on he/she wrote the script, remember export always returns with a zero exit status.

If the expression in if will exit with zero exit status then the if body will usually execute. The exit status of if command is the exit status of last command executed (as useall look at the manpage for more information in groups).

if true; then
   montana dont care;
   montana dont care;
   false
fi 
#  exits with exit status of false

Thus the programmer him/herself can insert || true to make the exit status of if expression to zero. Once writing bash scripts for build instructions as it seems you are you’ll run into this more and more.

1 Like

thanks for the code examples, this makes sense now, even looking at manage could not find this as Montana thank you

Imprint