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

Time:05-02

I love kubernetes and sometimes i found people using: '>-' in their yaml manifests and i wanted to know why and what is the difference with '|-' that is used to add files in configmaps or secret for example.

Thanks in advance

EDIT: I have read this https://yaml-multiline.info/ as suggested in the comments section by @zerkms but I still dont get the main differences. And why do we use "-"

CodePudding user response:

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 :)

CodePudding user response:

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 -.

  • Related