C.W.K.
Stream
Lesson 05 of 08 · published

Multi-Line Strings — | Literal vs > Folded

~12 min · yaml, strings, block-scalars

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Two block scalar styles, plus indicators

| — literal: keep every newline

Pipe means 'preserve newlines exactly.' Use this for shell scripts, code blocks, multi-paragraph descriptions where line breaks matter.

> — folded: join with spaces, blank line = paragraph break

Greater-than means 'fold consecutive non-empty lines into one with a space, treat blank lines as paragraph breaks.' Use this for prose paragraphs that should wrap freely on output but read narrow in the source.

Chomping indicators

Append - or + after | or > to control the trailing newline:

  • | or >clip (default). One trailing newline.
  • |- or >-strip. No trailing newline.
  • |+ or >+keep. All trailing newlines.

Indentation indicator (rare but useful)

|2 means 'block scalar with 2-space indent inside.' Useful when the content's first line itself begins with whitespace and the parser would otherwise miscount.

Folded mode silently corrupts code: if you put a Python or YAML snippet inside >, all your line breaks become spaces and you get a syntactically broken blob. Always use | for code blocks; use > only for prose where wrap-on-render is fine.

Code

| literal — preserves newlines·yaml
script: |
  #!/usr/bin/env bash
  set -euo pipefail

  echo "hello"
  for i in 1 2 3; do
    echo "$i"
  done
# Parses as a single string with literal newlines preserved.
> folded — joins lines into paragraphs·yaml
description: >
  Markup Quest covers four data and document
  formats every developer touches every day.

  This second paragraph is separated by the
  blank line above. Inside a paragraph, lines
  are joined into one with spaces.
# Parses as: "Markup Quest covers four... every day.\n\nThis second paragraph...\n"
Chomping — strip vs keep vs clip (default)·yaml
clip:  |
  line one
  line two
# Result: "line one\nline two\n" — one trailing \n

strip: |-
  line one
  line two
# Result: "line one\nline two" — no trailing \n

keep:  |+
  line one
  line two


# Result: "line one\nline two\n\n\n" — all trailing \n's preserved
Real-world: Kubernetes ConfigMap with a script·yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: bootstrap-script
data:
  init.sh: |
    #!/usr/bin/env bash
    set -euo pipefail
    apt-get update
    apt-get install -y curl jq
    curl -fsSL https://example.com/setup | bash

External links

Exercise

Write the same 5-line Bash script three ways: with |, with >, and with |-. Parse each with yq and read the parsed scalar. Notice which loses the line breaks (the > version) and which loses the trailing newline (|-). Decide which you'd reach for in a Kubernetes ConfigMap.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.