Variables in Bash

From the Bash Scripting cheat sheet · Variables & Quoting · verified Jul 2026

Variables

Assignment, usage, and scope.

bash
# Quick Reference
name="Sam"          # NO spaces around =
echo "$name"        # use with $
echo "${name}"      # braces for clarity/adjacency
💡 No spaces around = in assignments: name="Sam" (name = "Sam" runs a command).
⚡ Use ${var} braces when the name touches other text: ${name}_backup.
📌 Declare function variables with local so they do not leak into the global scope.
🟢 export makes a variable visible to child processes; plain assignment stays in the shell.
variablesscope

Continue with Bash Scripting

Save the full cheat sheet or work through every related task.

More Bash tasks

Back to the full Bash Scripting cheat sheet