Quoting controls how Bash splits text into command arguments. The same variable can become two words or one value.

Program

Play the script to see unquoted text split on a space, then quoted text stay together.

quoting.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

item="daily report"
printf '<%s>\n' $item
printf '<%s>\n' "$item"
  1. item ← daily report

    3item="daily report"4printf '<%s>\n' $item
    values this stepdaily reportitem
  2. printf '<%s> ' $item

    3item="daily report"4printf '<%s>\n' $item5printf '<%s>\n' "$item"
    output<daily>
    <report>
    values this stepdaily reportitemdaily, reportwords
  3. printf '<%s> ' "$item"

    4printf '<%s>\n' $item5printf '<%s>\n' "$item"
    output<daily report>
    values this stepdaily reportitemdaily reportwords

Follow the Words

  1. item starts as daily report.
  2. The unquoted $item is split on the space.
  3. The first printf receives two arguments: daily and report.
  4. The quoted "$item" stays as one argument.
  5. The second printf prints one line: <daily report>. | run | argument flow | output | | --- | --- | --- | | unquoted | daily report -> daily, report | <daily>, then <report> | | quoted | daily report stays one value | <daily report> |
word splitting After unquoted expansion, Bash splits text into words using whitespace.
double quotes Double quotes preserve spaces while still allowing variable expansion.

Exercise: quoting.sh

Reproduce the two unquoted lines and the one quoted line, then change item to another two-word value and predict the printed lines.