02. Variables
What is a variable?¶
A variable is a name that stores a value (string, number, filename, etc.). In Bash you assign with = (no spaces around =) and reference with a leading $.
Basic variable assignment & use¶
Run:
Tip: Quoting values preserves spaces and special characters:
Command substitution¶
Use $(...) (preferred) or backticks `...` to put command output into a variable.
Example: timestamped backup filename
When executed on 2022-02-09 it creates:
Why quote $OF? Quoting prevents word-splitting if the filename contains spaces.
Global vs Local variables (scope)¶
-
Global (script-level) variables are accessible anywhere in the script (including inside functions) unless overridden.
-
Local variables declared with
localare scoped to the function where they are defined.
Example:
#!/bin/bash
VAR="global variable" # global
function myfunc {
local VAR="local variable" # local to myfunc
echo "inside function: $VAR"
}
echo "before func: $VAR"
myfunc
echo "after func: $VAR"
Output:
local prevents the function from modifying the global VAR.
Exporting variables (environment variables)¶
export makes a variable available to child processes (subshells / commands you run).
#!/bin/bash
GREETING="hello"
export GREETING
# 02. Variables
python3 -c 'import os; print(os.getenv("GREETING"))' # prints "hello"
Useful variable patterns¶
Default values (parameter expansion)¶
Or use fallback in-place:
Read-only variables¶
Length, substring, replace¶
s="hello world"
echo "${#s}" # length
echo "${s:6:5}" # substring: 'world'
echo "${s/world/Bash}" # hello Bash
Arrays¶
Common pitfalls & best practices¶
-
No spaces around
=:VAR = valueis wrong; useVAR=value. -
Always quote expansions when the value may contain spaces: use
"$VAR"not$VAR. -
Use
$(...)for command substitution (clearer and nestable) instead of backticks. -
Prefer
localinside functions to avoid accidental global modification. -
Validate & sanitize user input (never trust untrusted input that ends up in filenames, commands, or URLs).
-
Use
set -u(treat unset variables as errors) andset -ewhere appropriate for safer scripts:-
-e: exit on first failing command -
-u: error on undefined variables -
-o pipefail: pipeline returns non-zero if any component fails
-
Example: a small backup script (safe-ish)¶
#!/bin/bash
set -euo pipefail
# configurable variables
BACKUP_DIR="/home/linuxconfig"
OUT_DIR="/tmp"
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
OF="${OUT_DIR}/myhome_${TIMESTAMP}.tar.gz"
# create backup
tar -czf "$OF" "$BACKUP_DIR"
echo "Backup created: $OF"
Run it and it prints the path of the created archive.
Quick reference / cheatsheet¶
-
Assign:
VAR=value -
Use:
"$VAR" -
Command output:
VAR="$(command)" -
Local:
local VAR="..."(inside a function) -
Export:
export VAR -
Default:
${VAR:-default}or${VAR:=default} -
Length:
${#VAR} -
Substring:
${VAR:pos:len}
Final notes¶
Variables are simple but powerful. Use quoting, set -euo pipefail for robustness, prefer local for function scope, and be explicit when exporting variables for child processes. With these practices you’ll write safer, clearer Bash scripts.