Add behaves differently according to the type of the LHS:
arrays: concatenate
number scalars: arithmetic addition
string scalars: concatenate
Use +=
as append assign for things like increment. Note that .a += .x
is equivalent to running .a = .a + .x
.
Given a sample.yml file of:
a:val: thingb:- cat- dog
then
yq eval '.a.b += ["cow"]' sample.yml
will output
a:val: thingb:- cat- dog- cow
Given a sample.yml file of:
a:- 1- 2b:- 3- 4
then
yq eval '.a + .b' sample.yml
will output
- 1- 2- 3- 4
Given a sample.yml file of:
a:- 1- 2
then
yq eval '.a + null' sample.yml
will output
- 1- 2
Given a sample.yml file of:
a:- dog: woof
then
yq eval '.a + {"cat": "meow"}' sample.yml
will output
- dog: woof- cat: meow
Given a sample.yml file of:
a:- 1- 2
then
yq eval '.a + "hello"' sample.yml
will output
- 1- 2- hello
Given a sample.yml file of:
a:- 1- 2b:- 3- 4
then
yq eval '.a = .a + .b' sample.yml
will output
a:- 1- 2- 3- 4b:- 3- 4
Given a sample.yml file of:
a: catb: meow
then
yq eval '.a = .a + .b' sample.yml
will output
a: catmeowb: meow
Given a sample.yml file of:
a: catb: meow
then
yq eval '.a += .b' sample.yml
will output
a: catmeowb: meow
If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
a: 3b: 4.9
then
yq eval '.a = .a + .b' sample.yml
will output
a: 7.9b: 4.9
If both the lhs and rhs are ints then the expression will be calculated with ints.
Given a sample.yml file of:
a: 3b: 4
then
yq eval '.a = .a + .b' sample.yml
will output
a: 7b: 4
Given a sample.yml file of:
a: 3
then
yq eval '.a += 1' sample.yml
will output
a: 4
Adding to null simply returns the rhs
Running
yq eval --null-input 'null + "cat"'
will output
cat