Quiz Entry - updated: 2026.06.20
How do you create and assign variables in Bash?
Assign with name=value and absolutely no spaces around the = — name=42, not name = 42.
The no-spaces rule is the #1 beginner trap, and it exists because of how bash parses a line: it splits on spaces first. With spaces, name = value looks like the command name with arguments = and value, so bash tries to run a program called name. name=value (no spaces) is the only thing bash recognizes as an assignment.
name=value # correct
message="Hello World" # quote values containing spaces
name = value # WRONG: runs command 'name'
name =value # WRONG: runs 'name' with arg '=value'
name= value # WRONG: sets name empty, then runs 'value'
Variable naming rules:
- Start with letter or underscore
- Can contain letters, numbers, underscores
- Case-sensitive (
Name≠name) - Convention: lowercase for local, UPPERCASE for exported
Examples:
# Simple assignment
greeting="Hello"
count=42
# With spaces - MUST quote
message="Hello World"
# From command output
today=$(date +%Y-%m-%d)
files=$(ls *.txt)
Tip: Always quote values containing spaces or special characters.