Search code examples
arraysyamlyq

How to test for array object presence with bash variable using yq


Given the file test.yaml:

[alpha, bravo, charlie, delta]

and the variable:

echo $WORD
# bravo

How do we test whether test.yaml contains $WORD?

I have tried yq 'contains(["$WORD"])' test.yaml, but this yields false.

I am using Mike Farah's yq.


Solution

  • Following pmf's guidance, the solution is to:

    WORD="$WORD" yq 'contains([strenv(WORD)])' test.yaml
    # true
    

    Source: https://mikefarah.gitbook.io/yq/operators/env-variable-operators#read-string-environment-variable