Bash variables hold strings. Assignment has no spaces around =. Use $name or ${name} to read values—compare with Python's clearer assignment rules.
Assignment and echo
name="Ada"
count=3
echo "Hello, $name"
echo "count is ${count}"Quotes prevent word-splitting on spaces. Without quotes, multiple words become multiple arguments.
Read-only and export
readonly PI=3.14
export APP_ENV=dev
echo "$APP_ENV"export puts variables into the environment child processes inherit—covered more in env-vars lesson.
Command substitution
today=$(date +%F)
echo "Today is $today"
lines=$(wc -l < notes.txt)
echo "notes.txt has $lines lines"$(...) runs a command and captures stdout—like running a function that returns text.
Important interview questions and answers
- Q: Spaces around = in assignment?
A: Forbidden for simple assignment—name=Adatries to run commandAda. - Q: $name vs ${name}?
A: Braces disambiguate names:${file}_backupvs$file_backup.
Self-check
- Why is name = Ada invalid?
- What syntax runs date and stores its output?
Pitfall: No spaces around = in assignments—count = 3 runs command =.
Interview prep
- Assignment spacing rule?
No spaces around = in name=value assignments.
- Command substitution syntax?
$(command) captures stdout as text.