Two ways to make a shortcut
An alias is a string substitution. alias ll='ls -lah' means "when I type ll, run ls -lah." A function is a small program. It takes arguments, has its own logic, can call other commands.
Alias rules
alias name='command'— definition.- No spaces around
=. - Quote the value (single quotes preferred).
- Aliases can't take arguments naturally — they're plain text replacement.
unalias name— remove.aliaswith no args — list every alias.
Function rules
name() { commands; }— POSIX form.function name { commands; }— bash/zsh.$1,$2, ... are arguments.$@all of them.local var=valuekeeps a variable inside the function.unset -f name— remove.
When to use which
- Need to add a flag to a command? → alias.
- Need to manipulate arguments? → function.
- More than one line? → function.
- Quick one-letter shortcut for a long command? → alias.
Common patterns
alias ll='ls -lah'
alias gst='git status'
alias gco='git checkout'
alias ports='lsof -iTCP -sTCP:LISTEN -P -n'
mkcd() { mkdir -p "$1" && cd "$1"; }
extract() {
case "$1" in
*.tar.gz|*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.tar.bz2) tar xjf "$1" ;;
*) echo 'unknown archive' ;;
esac
}