Aliases and Small Functions

An alias gives a long command a short name, such as ll for ls -lah, and a small function does the same for a job that needs the word you type after it, such as mkcd to make a folder and go into it.

Apprentice · Unix · Linux · Updated

After this lesson you can

  • Make an alias with alias name='command', see what a name stands for with type, and remove an alias with unalias
  • Write a small function, using "$1" for the word typed after its name, when an alias can't do the job

Key takeaways

  • alias name='command' makes a short name for a longer command. No spaces around the =, and quotes around the command.
  • alias on its own lists every alias, type shows what a name stands for, and unalias removes one.
  • An alias only fills in the start of a command. When the word you type is needed in the middle, or twice, write a small function, where "$1" is the first word after its name.
  • Aliases and functions you type at the prompt last until the terminal closes. The next lesson makes them stay.

Every morning you open the terminal and type the same few things: ls -lah to see what's in a folder, the long path to your to-do list, mkdir for a new folder and then cd into it, typing its name twice. A café regular doesn't order "a large oat-milk latte with an extra shot" every day. They say "the usual", and the barista knows. The shell can learn your usuals too. Developers, IT techs and data analysts all keep a handful of them, and they're often the first thing people set up on a new computer.

These lines make this lesson's practice folder: a to-do list, a hidden note and a folder for photos. They're safe to run again.

mkdir -p ~/terminal-practice/shortcuts && cd ~/terminal-practice/shortcuts
printf 'water the plants\ncall the dentist\nbuy coffee beans\n' > todo.txt
printf 'Saturday: farmers market at 9\n' > .weekend-plans.txt
mkdir -p photos

alias: a short name for a long command

An alias is a nickname for a command: a name, and the command it stands for.

alias todo='cat ~/terminal-practice/shortcuts/todo.txt'

Read it "alias todo is: cat, then the path to the list". Now todo works from any folder. Go home with cd and try it:

cd
todo
water the plants
call the dentist
buy coffee beans

Two rules trip people up: no spaces around the =, and quotes around the command, because it has spaces in it. Single quotes are the safest. With spaces around the =, bash reads each piece as the name of an alias to look up:

alias todo = 'cat todo.txt'
bash: alias: todo: not found
bash: alias: =: not found
bash: alias: cat todo.txt: not found

On a Mac, "alias" is also the name for a Finder shortcut to a file. That's a different thing: a shell alias lives in your terminal and stands for a command.

Ubuntu made some already

Go back into the practice folder with cd ~/terminal-practice/shortcuts, and ask what ll is. type, from Getting Help, says what any name stands for:

type ll
ll is aliased to `ls -alF'

alias on its own lists every alias the shell knows:

alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias todo='cat ~/terminal-practice/shortcuts/todo.txt'

Ubuntu made every one of them except todo. Skip the long alert; the others are short. ls is an alias for itself with --color=auto added, which is why folder names show up in color. A Mac's zsh starts with none of these, so ll says command not found there until you make it.

Making an alias with a name that's taken replaces the old one. Here's ll with -h, for sizes you can read at a glance (Looking Around: ls):

alias ll='ls -lah'
ll
total 20K
drwxr-xr-x  3 sam sam 4.0K Nov  9 05:22 .
drwxr-xr-x 10 sam sam 4.0K Nov  9 05:22 ..
-rw-r--r--  1 sam sam   30 Nov  9 05:22 .weekend-plans.txt
drwxr-xr-x  2 sam sam 4.0K Nov  9 05:22 photos
-rw-r--r--  1 sam sam   51 Nov  9 05:22 todo.txt

unalias and a name removes that alias. After that, Ubuntu tries to help with the name, which belongs to a program you don't need:

unalias todo
todo
Command 'todo' not found, but can be installed with:
sudo apt install devtodo

When an alias isn't enough: a small function

Making a folder and going into it takes two commands, mkdir -p photos/2026/november and then cd photos/2026/november, with the long name typed twice. An alias can't help here. It only fills in the start of a command, and what you type after it lands at the end, but this job needs the folder's name in two places. That's what a function is for: a few commands kept under one name. Type it line by line, or paste all four lines at once:

mkcd() {
  mkdir -p "$1"
  cd "$1"
}

After the first line, bash starts each line with > until the closing brace. Here > means "keep going", not "into a file". Read the function from the top. mkcd() (read "mkcd, brackets") starts a function named mkcd, and its commands go between the braces, { and }. $1 (read "dollar one") is the first word you type after the function's name, and the quotes around "$1" keep a name with spaces in one piece. Now one command does both jobs:

mkcd photos/2026/november
pwd
/home/sam/terminal-practice/shortcuts/photos/2026/november

type mkcd prints the function back to you. Functions can do far more, and Functions: Naming a Job, in Module 6, covers them in full. For a shortcut, two or three lines like these are plenty.

Gone when you close the window

Aliases and functions you type at the prompt live in this shell only. Close the terminal, open a new one, and mkcd is gone. The next lesson, Startup Files: .bashrc, .zshrc and source, puts them in a file the shell reads every time it starts.

On a Mac, alias, unalias and functions work the same way in zsh, and type words its answer a little differently.

Try it in the terminal

Terminal Quest: The Clockwork Workshop, goal 5: played right on the lesson page.

Winding the clock in Ada's workshop takes ./clock --wind --twice every time. Give it a short name, tick, and wind it with one word.

Check yourself

2 questions to try on the lesson page.

So what

Any command you type often can become one short word of your own: an alias for a command, and a small function when the word you type has to go somewhere in the middle.

Key terms

Cheat sheets

Related