2021-10-30 03:14:39 +00:00
# Boolean Operators
2021-11-03 04:00:28 +00:00
The `or` and `and` operators take two parameters and return a boolean result.
2021-10-30 03:14:39 +00:00
2021-11-03 04:00:28 +00:00
`not` flips a boolean from true to false, or vice versa.
2021-10-30 03:14:39 +00:00
`any` will return `true` if there are any `true` values in a array sequence, and `all` will return true if _all_ elements in an array are true.
`any_c(condition)` and `all_c(condition)` are like `any` and `all` but they take a condition expression that is used against each element to determine if it's `true` . Note: in `jq` you can simply pass a condition to `any` or `all` and it simply works - `yq` isn't that clever..yet
These are most commonly used with the `select` operator to filter particular nodes.
2022-03-28 03:24:57 +00:00
## Related Operators
2022-03-29 00:12:22 +00:00
- equals / not equals (`==`, `!=` ) operators [here ](https://mikefarah.gitbook.io/yq/operators/equals )
- comparison (`>=`, `<` etc) operators [here ](https://mikefarah.gitbook.io/yq/operators/compare )
- select operator [here ](https://mikefarah.gitbook.io/yq/operators/select )
2022-03-28 03:24:57 +00:00
2021-10-30 03:14:39 +00:00
## `or` example
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input 'true or false'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
## `and` example
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input 'true and false'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Matching nodes with select, equals and or
Given a sample.yml file of:
```yaml
- a: bird
b: dog
- a: frog
b: bird
- a: cat
b: fly
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- a: bird
b: dog
- a: cat
b: fly
```
## `any` returns true if any boolean in a given array is true
Given a sample.yml file of:
```yaml
- false
- true
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'any' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
## `any` returns false for an empty array
Given a sample.yml file of:
```yaml
[]
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'any' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## `any_c` returns true if any element in the array is true for the given condition.
Given a sample.yml file of:
```yaml
a:
- rad
- awesome
b:
- meh
- whatever
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] |= any_c(. == "awesome")' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: true
b: false
```
## `all` returns true if all booleans in a given array are true
Given a sample.yml file of:
```yaml
- true
- true
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'all' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
## `all` returns true for an empty array
Given a sample.yml file of:
```yaml
[]
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'all' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
## `all_c` returns true if all elements in the array are true for the given condition.
Given a sample.yml file of:
```yaml
a:
- rad
- awesome
b:
- meh
- 12
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] |= all_c(tag == "!!str")' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: true
b: false
```
## Not true is false
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input 'true | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Not false is true
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input 'false | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
## String values considered to be true
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '"cat" | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Empty string value considered to be true
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '"" | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Numbers are considered to be true
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '1 | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Zero is considered to be true
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '0 | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
```
## Null is considered to be false
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input '~ | not'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
2021-11-03 04:00:28 +00:00