Bash Variables
A variable stores a value under a name so you can reuse it. In Bash you create a variable by assigning to it and read it with a $ in front of the name.
Assigning and reading
name="Ada"
count=3
greeting="Hello, $name"
echo "$greeting"
echo "You have $count new messages"
echo "${name}'s inbox"Hello, Ada
You have 3 new messages
Ada's inbox
Rules for assignment:
- No spaces around
=.name = "Ada"runs a command callednamewith the arguments=andAda. - Names may contain letters, digits and
_, and can't start with a digit. They are case-sensitive. ${name}is the full form of$name. You need the braces when letters follow the name:"${name}s"versus"$names"(a different, empty variable).
file="report"
echo "$file_2026.pdf"
echo "${file}_2026.pdf"
city = "Paris".pdf
report_2026.pdf
/w/main.sh: line 4: city: command not found
The first echo looked for a variable called file_2026 (unset, so empty). The last line shows the classic spaces-around-= error.
Unset and empty variables
Reading a variable that was never set gives an empty string, silently. That's convenient and also a common source of bugs; set -u (error-handling lesson) turns it into an error. Bash has expansions to supply defaults:
| Expansion | Result |
|---|---|
${var:-default} |
default if var is unset or empty (var itself unchanged) |
${var:=default} |
Same, and also assigns default to var |
${var:?message} |
Error with message if unset or empty |
${var:+other} |
other if var is set and non-empty, else nothing |
echo "port: ${PORT:-8080}"
PORT=3000
echo "port: ${PORT:-8080}"
echo "log level: ${LEVEL:=info}, now LEVEL=$LEVEL"
DEBUG=1
echo "flags: ${DEBUG:+--verbose}"
unset PORT
echo "after unset: '${PORT}'"port: 8080
port: 3000
log level: info, now LEVEL=info
flags: --verbose
after unset: ''
unset NAME removes a variable.
Command output in variables
Combine variables with command substitution $(...) to store results:
printf 'ada\nlinus\ngrace\n' > users.txt
user_count=$(wc -l < users.txt)
first_user=$(head -n 1 users.txt)
today=$(date -d 2026-03-15 +%A)
echo "$user_count users, first is $first_user, report day: $today"3 users, first is ada, report day: Sunday
Arithmetic
All Bash variables hold strings, but $(( ... )) evaluates integer arithmetic, and inside it you can leave out the $:
apples=7
pears=5
total=$(( apples + pears ))
echo "total: $total"
echo "half: $(( total / 2 )), remainder: $(( total % 5 ))"
(( total++ ))
echo "after ++: $total"
(( total += 10 ))
echo "after += 10: $total, squared: $(( total ** 2 ))"total: 12
half: 6, remainder: 2
after ++: 13
after += 10: 23, squared: 529
Division is integer division (12 / 5 is 2). For decimals use bc or awk (strings lesson).
Read-only, local and declared variables
| Command | Effect |
|---|---|
readonly MAX=10 |
A constant: can't be changed or unset |
local x=1 |
A variable that exists only inside a function |
declare -i n=5 |
An integer variable: assignments are evaluated arithmetically |
declare -l s / -u |
Lower-case / upper-case on assignment |
export VAR |
Pass the variable to child processes (environment lesson) |
readonly MAX_RETRIES=3
MAX_RETRIES=5
echo "still $MAX_RETRIES"
declare -i score=10
score=score*2+1
echo "score: $score"
declare -u code="abc-12"
echo "code: $code"/w/main.sh: line 2: MAX_RETRIES: readonly variable
still 3
score: 21
code: ABC-12
Special variables
Bash sets some variables automatically:
| Variable | Meaning |
|---|---|
$0 |
The script's name |
$1 … $9, ${10} |
Script or function arguments |
$# |
Number of arguments |
$@ |
All arguments (as separate words) |
$? |
Exit status of the last command |
$ |
PID of the current shell |
$! |
PID of the last background job |
$HOME, $PWD, $PATH |
Home directory, current directory, search path |
Tip: By convention, environment variables and constants are
UPPER_CASE, and your script's own variables arelower_case. That avoids accidentally overwriting important variables such asPATHorHOME.
Test yourself
Which line correctly assigns a variable?
- name = Ada
- $name=Ada
- name=Ada
- set name Ada
What does echo ${COLOR:-blue} print when COLOR is unset?
- Nothing
- blue
- COLOR
- An error
What is $(( 17 / 5 )) in Bash?
- 3.4
- 3
- 4
- 2