Variables
Variables are containers for storing data in Bash. They're fundamental to any programming or scripting language, and Bash is no exception. In this lesson, you'll learn how to create variables, assign values, follow naming conventions, and work with variables effectively. Unlike many languages, Bash doesn't require variable declarations - just assign a value and use it!
Variable Assignment
In Bash, you create a variable simply by assigning a value to it. The syntax is straightforward: variable_name=value. Note that there are no spaces around the equals sign!
Click Run to execute your code
= in variable assignment!-
name="Alice" ✓ Correct-
name = "Alice" ✗ Wrong (Bash will try to run name as a command)
Accessing Variables
To use a variable's value, prefix it with the dollar sign $. Bash will replace $variable_name with the actual value stored in the variable.
# Assign a value
name="Alice"
age=25
# Access the value
echo "My name is $name"
echo "I am $age years old"
# Can also use braces for clarity
echo "Hello, ${name}!"
echo "Age: ${age}"
${variable} is often clearer and required in certain situations. For example, ${name}_file works, but $name_file tries to access a variable called name_file!
Variable Naming Rules
Bash has specific rules for variable names. Understanding these rules helps you write valid code and avoid errors.
Click Run to execute your code
| Rule | Valid | Invalid |
|---|---|---|
| Can contain letters, numbers, underscores | var1, my_var, VAR_NAME |
2var (can't start with number) |
| Case-sensitive | name and Name are different |
N/A |
| No spaces | my_var |
my var (space not allowed) |
| No hyphens | my_var |
my-var (hyphen not allowed) |
| Avoid reserved words | count, total |
if, then, else (reserved) |
Strings vs Numbers
Bash treats all variables as strings by default. Even numbers are stored as strings, but Bash can perform arithmetic when needed.
# Both are strings
text="Hello"
number="42"
# But Bash can do arithmetic
count=10
count=$((count + 1)) # Arithmetic expansion
echo $count # 11
# Or use let
let count=count+1
echo $count # 12
Unsetting Variables
To remove a variable (make it undefined), use the unset command. This is useful for cleaning up or resetting values.
# Create a variable
temp="temporary data"
echo $temp # temporary data
# Unset it
unset temp
echo $temp # (empty - variable no longer exists)
# Check if variable is set
if [ -z "${temp:-}" ]; then
echo "Variable is unset or empty"
fi
Common Mistakes
1. Spaces around equals sign
# Wrong - spaces cause error
name = "Alice" # Error: command not found
# Correct - no spaces
name="Alice"
2. Forgetting $ when accessing variables
# Wrong - prints literal "name"
name="Alice"
echo name # Output: name
# Correct - use $ to access value
echo $name # Output: Alice
3. Using hyphens in variable names
# Wrong - hyphens not allowed
my-var="value" # Error: command not found
# Correct - use underscore
my_var="value"
4. Starting variable name with number
# Wrong - can't start with number
2nd_place="silver" # Error: command not found
# Correct - start with letter or underscore
second_place="silver"
_2nd_place="silver" # Also valid
5. Not using braces when needed
# Ambiguous - Bash looks for "name_file" variable
name="test"
echo $name_file # Wrong: tries to find variable "name_file"
# Correct - use braces for clarity
echo ${name}_file # Output: test_file
Exercise: Work with Variables
Task: Create a script that uses variables to store and display information about yourself!
Requirements:
- Create variables for: first name, last name, age, city
- Print a greeting using your full name
- Print your age and city
- Use proper variable naming conventions (lowercase with underscores)
- Unset one variable and show it's no longer available
Hint: Remember no spaces around =, and use $ to access variable values!
Show Solution
#!/bin/bash
# Personal information script
# Create variables
first_name="Alex"
last_name="Johnson"
age=28
city="San Francisco"
# Display information
echo "Hello! My name is $first_name $last_name"
echo "I am $age years old"
echo "I live in $city"
echo ""
# Unset a variable
unset city
echo "After unsetting city:"
echo "City variable: '${city:-not set}'"
Summary
- Assignment:
variable=value(no spaces around=) - Access: Use
$variableor${variable}to get the value - Naming: Letters, numbers, underscores; case-sensitive; can't start with number
- All Strings: Bash treats all variables as strings, but can do arithmetic
- Unset: Use
unset variableto remove a variable - Braces: Use
${var}when clarity is needed or for concatenation - No Declaration: Variables don't need to be declared - just assign and use
What's Next?
Great progress! Now that you understand basic variables, let's explore Variable Expansion - powerful techniques for working with variables, including default values, error handling, and string manipulation. You'll learn patterns like ${var:-default} that make your scripts more robust!
Enjoying these tutorials?