How to use shell variables
Overview
Shell variables give names to text values. Environment variables are exported so programs started by the shell can read them.
Examples
course='Linux basics'
echo "$course"
file="$HOME/notes.txt"
printf '%s\n' "$file"
export EDITOR=nano
unset course
Rules and expansions
- Do not put spaces around
=in an assignment - Use
$nameor${name}to expand a value - Double quotes allow expansion while preserving spaces
- Single quotes keep text literal
$HOME,$PATH, and$?are common shell valuesexportpasses a variable to child processes
Tips and safety
Quote variable expansions as "$file" unless you specifically need word splitting or wildcard expansion.
Do not store secrets in shell history or commit them in scripts. Environment variables can also be visible to processes and diagnostic tools.