1

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"?

user1244932
  • 7,352
  • 5
  • 46
  • 103
  • 1
    Not directly related to your question: `set -e` is redundant. See [`jobs..steps[*].shell`](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell). – Azeem Feb 10 '23 at 12:30

1 Answers1

3

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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
hyperupcall
  • 869
  • 10
  • 21
  • Edited to add quotes. Without them, you get `[ -z ]` instead of `[ -z '' ]` in the unset case; sure, they're both true, but they're true _for different reasons_, as `[ -z ]` is equivalent to `[ -n "-z" ]` – Charles Duffy Feb 10 '23 at 12:22
  • You might also want to add the link of the GHA [Preinstalled Software](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software) in your answer to reference those different Bash versions. – Azeem Feb 10 '23 at 12:28