Expansion and Data
Quoting
Words vs One Value
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"
item ← daily report
3item="daily report"4printf '<%s>\n' $itemvalues this stepdaily reportitemprintf '<%s> ' $item
3item="daily report"4printf '<%s>\n' $item5printf '<%s>\n' "$item"output<daily> <report>values this stepdaily reportitemdaily, reportwordsprintf '<%s> ' "$item"
4printf '<%s>\n' $item5printf '<%s>\n' "$item"output<daily report>values this stepdaily reportitemdaily reportwords
Follow the Words
itemstarts asdaily report.- The unquoted
$itemis split on the space. - The first
printfreceives two arguments:dailyandreport. - The quoted
"$item"stays as one argument. - The second
printfprints one line:<daily report>. | run | argument flow | output | | --- | --- | --- | | unquoted |daily report->daily,report|<daily>, then<report>| | quoted |daily reportstays 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.