Bash Variables & Parameters
Variables are essential for storing and manipulating data in Bash scripts. Bash provides several types of variables and special parameters that give you access to script arguments and other useful information.
Basic Variables
Variable Assignment
#!/bin/bash
# Assign a value to a variable
name="John Doe"
# Use the variable
echo "Hello, $name"
Rules for Variable Names
- Can contain letters, numbers, and underscores
- Must start with a letter or underscore
- Are case-sensitive
- By convention, use uppercase for constants and lowercase for variables
Variable Expansion
Basic Expansion
fruit="apple"
echo "I have an $fruit" # I have an apple
Braces for Clarity
fruit="apple"
echo "I have ${fruit}s" # I have apples (clearer than $fruits)
Default Values
# Use default if variable is unset or empty
echo "${name:-Guest}" # Guest if name is unset
echo "${name:=Guest}" # Guest if name is unset, and assigns it
Special Parameters
Parameter | Description |
---|---|
$0 |
Name of the script |
$1, $2, ... $9 |
Positional parameters (arguments) |
$# |
Number of arguments |
$* |
All arguments as a single string |
$@ |
All arguments as separate strings |
$? |
Exit status of last command |
$$ |
Process ID of the script |
$! |
Process ID of last background command |
Command Line Arguments
Accessing Arguments
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"
Shifting Arguments
#!/bin/bash
echo "First argument: $1"
shift # Shift arguments left
echo "After shift, first argument: $1"
Arrays
Creating Arrays
# Indexed array
fruits=("Apple" "Banana" "Cherry")
# Associative array (Bash 4+)
declare -A user
user=([name]="John" [age]=30)
Accessing Array Elements
# Indexed array
echo ${fruits[0]} # Apple
echo ${fruits[@]} # All elements
echo ${#fruits[@]} # Number of elements
# Associative array
echo ${user[name]} # John
Environment Variables
Common Environment Variables
echo "User: $USER"
echo "Home: $HOME"
echo "Path: $PATH"
echo "Current directory: $PWD"
echo "Shell: $SHELL"
Setting Environment Variables
# For current session
export MY_VAR="value"
# For just the script
MY_SCRIPT_VAR="script_value" ./script.sh
Quoting Rules
Different Quote Types
# Single quotes - no expansion
echo '$USER' # $USER
# Double quotes - expansion happens
echo "$USER" # john
# No quotes - word splitting
echo hello world # hello world
Parameter Expansion Operations
Expression | Description |
---|---|
${var:-default} |
Use default if var is unset or empty |
${var:=default} |
Use default if var is unset or empty, and assign it |
${var:?message} |
Display message and exit if var is unset |
${var:+alternate} |
Use alternate if var is set |
${#var} |
Length of var |
${var:offset:length} |
Substring expansion |
${var#pattern} |
Remove shortest leading pattern |
${var##pattern} |
Remove longest leading pattern |
${var%pattern} |
Remove shortest trailing pattern |
${var%%pattern} |
Remove longest trailing pattern |
${var/pattern/replacement} |
Replace first match |
${var//pattern/replacement} |
Replace all matches |
Best Practices
- Quote variables to prevent word splitting and globbing
- Use
${var}
instead of$var
when adjacent text could cause confusion - Use
readonly
for constants - Prefer local variables in functions
- Use descriptive variable names
Next Steps
Continue learning with:
- Control Flow - Conditionals and loops in Bash
- Functions - Creating reusable code blocks