a=375 # assign (NO spaces around =)hello=$a # assign from variableecho $hello # reference: 375echo ${hello} # preferred form with braces: 375right_now="$(date +"%x %r")" # command substitution
Rules:
Variable names: start with letter, no whitespace or punctuation
Reference: $varname or ${varname} (braces = more robust)
No spaces around = when assigning
Environment variables
export MYVAR=Value # export to child processesprintenv # list all env varsecho $SHELL # read a specific env var
Quoting
Quote type
What it does
"double"
Interprets $var and \ — protects special chars except those
'single'
Treats EVERYTHING literally — no variable substitution
\char
Escape single character
var="hello world"echo "$var" # → hello world (preserved as one argument)echo '$var' # → $var (literal, no substitution)echo $var # → hello world (but word-splitting can cause issues)
Always quote variable references: "$var" not $var (prevents word splitting).
Functions
# Definition must come BEFORE usesystem_info(){ echo "function called" # must have at least one valid command}# Call (no brackets)system_info
Positional parameters
greet(){ local name="$1" # local = function-scoped variable local age="$2" echo "Hello $name, age $age"}greet "Alice" 30