C.W.K.
Stream
Lesson 04 of 10 · published

Aliases vs Functions

~10 min · alias, function

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

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.
  • alias with 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=value keeps 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
}

Code

Add and use·bash
alias ll='ls -lah'
ll ~
mkcd() { mkdir -p "$1" && cd "$1"; }
mkcd ~/sandbox/2026/05
pwd

External links

Exercise

Add three aliases to .zshrc: ll='ls -lah', gst='git status', ports='lsof -iTCP -sTCP:LISTEN -P -n'. Then add a mkcd function. exec zsh to reload, test each.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.