yaml中的'>-'和'|-'有什么区别?

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

我想知道“>-”和“|-”之间的确切区别,尤其是在 kubernetes yaml 清单中

好的,我知道 > 和 | 之间的一个主要区别来自这里: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.

例如:

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

事实上,“>”在我的理解中,相当于 bash 脚本末尾的转义符 '\' 例如

如果有人能告诉我在 kubernetes yaml 清单中使用的“-”是什么,它将完成我的理解:)

折叠块标量 (>) 中的换行符受行折叠影响,文字块标量 (|) 中的换行符不受。

行折叠用 space 替换 non-empty 行之间的单个换行符,并且在空行的情况下,减少周围 non-empty 行之间的换行符数量一:

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

  three
  four


  five

当至少一行缩进更多时,行之间不会发生行折叠,即开头包含白色space,这不是块的一般缩进的一部分:

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

   five

|> 之后添加 - 将从最后一行删除换行符:

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

  two

相比之下,| 发出每个换行符 as-is,如果您使用 -.

,唯一的例外是最后一个换行符