'What is the difference between '>-' and '|-' in yaml?

I wanted to know exactly what is the difference between '>-' and '|-' especially in kubernetes yaml manifests



Solution 1:[1]

Newlines in folded block scalars (>) are subject to line folding, newlines in literal block scalars (|) are not.

Line folding replaces a single newline between non-empty lines with a space, and in the case of empty lines, reduces the number of newline characters between the surrounding non-empty lines by one:

a: > # folds into "one two\nthree four\n\nfive\n"
  one
  two

  three
  four


  five

Line folding does not occur between lines when at least one line is more indented, i.e. contains whitespace at the beginning that is not part of the block's general indentation:

a: > # folds into "one\n  two\nthree four\n\n five\n"
  one
    two
  three
  four

   five

Adding - after either | or > will strip the newline character from the last line:

a: >- # folded into "one two"
  one
  two
b: >- # folded into "one\ntwo"
  one

  two

In contrast, | emits every newline character as-is, the sole exception being the last one if you use -.

Solution 2:[2]

Ok I got one main difference between > and | from here: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

Values can span multiple lines using | or >. Spanning multiple lines using a “Literal Block Scalar” | will include the newlines and any trailing spaces. Using a “Folded Block Scalar” > will fold newlines to spaces; it’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored.

Examples are:

include_newlines: |
            exactly as you see
            will appear these three
            lines of poetry

fold_newlines: >
            this is really a
            single line of text
            despite appearances

In fact the ">" is in my understanding, the equivalent of the escape characters '\' at the end of a bash script for example

If one can tell me what is the "-" used for in kubernetes yaml manifests it will complete my understanding :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 flyx
Solution 2 bguess