Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have this code in a yaml file. This file gets run when I run docker.

if [[ $EXITCODE -ne 0 && "$CI_COMMIT_BRANCH" == "staging" ]] || [[ $EXITCODE -ne 0 && "$CI_COMMIT_BRANCH" == "develop" ]]; then
  echo "Hello"

When I run these lines of code in my terminal it works as it should. But when I run this file via docker, I get this error

sh: 0: unknown operand

What is happening here? Why am I getting this error? I've tried using different syntax for the $EXITCODE and the 0 but still getting the same error

You ran the echo where, in your terminal or via docker? And where is that yaml, what is parsing it, compose, a CI tool? – BMitch Nov 30, 2021 at 14:58 Inside your docker container, run sh --version and let us know if it calls itself GNU bash, as well as the version, please. If your docker container's OS is not simply aliasing bash as sh, then Bashisms like double square brackets for testing, and double equals for equality will not work, as they're not POSIX compliant. See this link for common bashisms and how to translate to POSIX – Tyler Stoney Nov 30, 2021 at 15:06 If you have bash, I would avoid -eq and -ne inside [[ ... ]] if for no other reason than to improve readability. if (( $EXITCODE != 0)) && [[ "$CI_COMMIT_BRANCH" == "staging" ]], for example. In bash, though, -ne and -eq both trigger arithmetic evaluation, which means that even if EXITCODE is not defined, then the empty string produced by $EXITCODE will be treated as an implicit 0. – chepner Nov 30, 2021 at 16:13

Thank you everyone for your response!

So this works

if [[ "$EXITCODE" == "0" && "$CI_COMMIT_BRANCH" == "staging" ]] || [[ $EXITCODE -ne 0 && "$CI_COMMIT_BRANCH" == "develop" ]]; then
  echo "Hello"

I guess it didn't like -ne. However I have used

if [[ $EXITCODE -ne 0 ]]; then
  echo "Hello"

and it has worked on its own. I guess you can't use it when there are other conditions and it's using the == or !=

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.