Bash variable assignment
What you need to know
A variable gives a name to a value so you can reuse it. Bash scalar variables normally hold text. Assign with name=value (no spaces around =), then expand the value with "$name". A valid ordinary name starts with an ASCII letter (A–Z or a–z) or underscore and continues with ASCII letters, digits (0–9) or underscores. Names are case-sensitive.
Run each example separately as a new Bash script (Chapter 1). Tested with Bash 5.2.37 on Debian 13, default shell options; no personal configuration is required.
Store a name
Expected stdout:
SaraThe assignment stores Sara; the dollar sign requests its value. Double quotes keep the expanded text together as one argument.
Reuse and update a value
project='Daily report'printf 'Start: %s\n' "$project"project='Weekly report'printf 'Next: %s\n' "$project"Expected stdout:
Start: Daily reportNext: Weekly reportThe second assignment replaces the value. Quotes on the right allow a value with spaces. The format string stays fixed.
Separate a name from a suffix
file='report'printf '%s\n' "${file}.txt" "${file}_old"Expected stdout:
report.txtreport_oldBraces mark the variable name explicitly; without them, $file_old would refer to a different variable.
Common mistakes
name = Sara is not an assignment: Bash tries to execute a command called name with arguments. Use name=Sara. Avoid writing $name=Sara: the left side is the name, not its value.
Practice
Store Monthly summary in report, print it, change it to Annual summary, and print the new value.
Next lesson: Unset and empty variables