Search code examples
bashshellgithub-actions

test: -v: unary operator expected


I try to run such code via "github actions"

      - name: Run tests
        run: |
          set -e
          ...
          test -v A || export B=42
        shell: bash

and got error at line test -v A || export B=42:

test: -v: unary operator expected

I have no idea what is this about, if I run this command in my local bash all works as expected, what is wrong in my yaml code for "github actions"?


Solution

  • By any chance does your test happen to be running under a macOS CI environment? test -v tests if a variable has been set, but, it was only added in Bash 4.2.

    macOS by default comes with Bash 3.2, and that's the expected error if it doesn't recognize -v as a unary operator.

    To fix this, you can install the latest Bash, or use a different approach for testing for existence:

    [ -z "${A+x}" ] && export B=42