Variable Basics
Variables are containers for storing data that your scripts can use and manipulate. Understanding variables is fundamental to writing useful bash scripts.
What is a Variable?
A variable is a named storage location that holds a value. In bash:
NAME="Alice" # Create variable
echo $NAME # Access variable: prints "Alice"
Creating Variables
The syntax for creating variables is simple but strict:
# Correct - no spaces around =
VARIABLE="value"
# WRONG - spaces cause errors
VARIABLE = "value" # Error!
VARIABLE= "value" # Error!
VARIABLE ="value" # Error!
Variable Naming Rules
Follow these naming conventions:
| Rule | Valid | Invalid |
|---|---|---|
| Start with letter or underscore | NAME, _count | 1name, -var |
| Use letters, numbers, underscores | file_name2 | file-name, my.var |
| Case sensitive | Name ≠ NAME | - |
| Convention: UPPERCASE for constants | MAX_SIZE | - |
| Convention: lowercase for local vars | filename | - |
# Good variable names
username="alice"
FILE_PATH="/home/user"
_counter=0
item2="second"
# Bad variable names (will cause errors)
# 2fast="value" # Can't start with number
# my-var="value" # Hyphens not allowed
# my.var="value" # Dots not allowed
Accessing Variables
Use $ to access a variable's value:
NAME="Bob"
echo $NAME # Bob
echo "$NAME" # Bob (preferred)
echo "${NAME}" # Bob (explicit)
echo '$NAME' # $NAME (literal - single quotes)
Braces Syntax
Use ${VAR} when the variable name could be ambiguous:
FILE="report"
echo "$FILE_2024" # Looks for variable FILE_2024
echo "${FILE}_2024" # Correct: report_2024
Exercise: Create and Use Variables
Create a variable and display its value:
Reassigning Variables
Variables can be changed at any time:
COUNT=1
echo $COUNT # 1
COUNT=2
echo $COUNT # 2
COUNT=$((COUNT + 1))
echo $COUNT # 3
Unsetting Variables
Remove a variable with unset:
NAME="Alice"
echo $NAME # Alice
unset NAME
echo $NAME # (empty)
Read-Only Variables
Make a variable constant with readonly:
readonly PI=3.14159
PI=3.14 # Error: PI: readonly variable
Or use declare -r:
declare -r MAX_USERS=100
MAX_USERS=200 # Error: MAX_USERS: readonly variable
Command Substitution
Capture command output in a variable:
# Modern syntax (preferred)
CURRENT_DATE=$(date)
FILE_COUNT=$(ls | wc -l)
# Older syntax (backticks)
CURRENT_DATE=`date`
Exercise: Command Substitution
Store the output of a command in a variable:
Arithmetic with Variables
Bash supports integer arithmetic:
# Using $(( ))
A=5
B=3
SUM=$((A + B)) # 8
DIFF=$((A - B)) # 2
PRODUCT=$((A * B)) # 15
QUOTIENT=$((A / B)) # 1 (integer division)
REMAINDER=$((A % B)) # 2
echo "Sum: $SUM"
Increment and Decrement
COUNT=0
((COUNT++)) # Increment: COUNT is now 1
((COUNT--)) # Decrement: COUNT is now 0
((COUNT+=5)) # Add 5: COUNT is now 5
Default Values
Provide default values for unset variables:
# Use default if unset or empty
echo ${NAME:-"Unknown"} # "Unknown" if NAME is unset
# Set default if unset or empty
echo ${NAME:="Default"} # Sets NAME to "Default" if unset
# Use alternative if set
echo ${NAME:+"Has value"} # "Has value" if NAME is set
# Error if unset
echo ${NAME:?"Error: NAME required"} # Exits with error if unset
Checking if Variable is Set
# Check if variable exists
if [ -z "$NAME" ]; then
echo "NAME is empty or unset"
fi
if [ -n "$NAME" ]; then
echo "NAME has a value: $NAME"
fi
Key Takeaways
- Create variables with
VAR="value"(no spaces around=) - Access variables with
$VARor${VAR} - Variable names are case-sensitive
- Use
$()to capture command output - Use
$(( ))for arithmetic operations - Use
${VAR:-default}for default values - Variables are untyped - everything is essentially a string

