Shell Functions & Aliases — Terminal

An alias is a text abbreviation: alias ll='ls -la' (no spaces around =). When ll is the first word you type, the shell replaces it with ls -la, and any other…

Aliases and functions make long commands short. An alias is an abbreviation: the shell replaces a short word with its long text, then runs it. A function is a command of your own: it holds one step or more, and it can take arguments. They are like two features on your phone. An alias is like a keyboard text shortcut: you type "otw", and the phone replaces it with "on the way, wait for me". A function is like an automation called "Going home": you start it once and several steps run one after another, and you can give it an input, such as where to go. In a function, that input is called an argument.

Alias: a command abbreviation

For example, these three aliases:

alias ll='ls -la'
alias gs='git status'
alias gp='git push'

Here is the result in a bash terminal, in a ~/learning folder that holds a Git repo with no new changes:

$ type ll
ll is aliased to `ls -la'
$ alias
alias gp='git push'
alias gs='git status'
alias ll='ls -la'
$ gs
On branch main
nothing to commit, working tree clean

Function: a command with steps and arguments

Here are two functions, mkcd and deploy:

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

deploy() {
  npm run build
  git add -A
  git push
}

Here is mkcd used in the terminal:

$ pwd
/home/budi/learning
$ mkcd new-project
$ pwd
/home/budi/learning/new-project

You'll also see the one-line form, function mkcd() { mkdir -p "$1" && cd "$1"; }. It means the same. On one line, the last step ends with ; before the }.

Why an alias can't use $1

Guess first: you make alias newdir='mkdir -p $1 && cd $1', then type newdir new-project. Is the folder made, and do you end up inside it?

$ alias newdir='mkdir -p $1 && cd $1'
$ newdir new-project
mkdir: missing operand
Try 'mkdir --help' for more information.

An alias only pastes text. newdir new-project becomes mkdir -p $1 && cd $1 new-project. In a normal terminal (not inside a script or a function), $1 is empty, so mkdir -p gets no folder name and fails. Because mkdir failed, && cancels the cd, and new-project is left at the end, unused. No folder is made, and you don't move anywhere.

If you make newdir a function, $1 gets filled:

$ unalias newdir
$ newdir() { mkdir -p "$1" && cd "$1"; }
$ newdir new-project
$ pwd
/home/budi/learning/new-project

unalias newdir has to come first. While newdir is still an alias, bash also replaces the word newdir in the line that makes the function, and the result is syntax error near unexpected token.

Alias Function
Holds one replacement text one step or more
Arguments only added at the end $1, $2 usable anywhere
if, for logic no yes
Good for abbreviations: ll, gs a series of steps: mkcd, deploy

Keep them permanently in ~/.bashrc or ~/.zshrc

Aliases and functions typed in the terminal are gone once the terminal closes. To always have them, write them in your shell's settings file: ~/.bashrc for bash (the Linux default) or ~/.zshrc for zsh (the macOS default). Then reload that file in the terminal that's already open:

source ~/.zshrc     # or: source ~/.bashrc

A new terminal reads that file by itself when it opens. The details are in the Dotfiles lesson.

⚠️ Spaces, $1, quotes and clashing names

TL;DR: An alias is a text abbreviation: alias ll='ls -la' (no spaces around =). When ll is the first word you type, the shell replaces it with ls -la, and any other words after it stay. A name that isn't an alias, like pwd, runs as it is. A function is a command of your own with steps and arguments: mkcd() {, then each step on its own line with two spaces in front, then }. Inside a function, $1 holds the first argument. mkcd projects/new makes the folder with its parent folders (mkdir -p), then moves into it (cd). The && between them only runs cd if mkdir succeeded. An alias can't use $1, so for arguments use a function. Keep both in ~/.bashrc or ~/.zshrc, then reload with source ~/.zshrc. Check what a name means with type name.

⚡ Alias: an abbreviation

Write:

alias ll='ls -la'

Meaning: An alias is an abbreviation. When you type ll, the shell replaces it with ls -la, then runs it. No spaces around =.

⚡ Function: steps and arguments

Write:

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

Meaning: A function is a command of your own, with one step or more. $1 holds the first argument: the text you type after the function's name. So mkcd new-project makes the folder new-project and moves straight into it. && only runs cd if mkdir succeeded.

⚡ Why an alias isn't enough

Guess first: alias newdir='mkdir -p $1 && cd $1', then newdir new-project. Is the folder made?

No. An alias only pastes text, so the command becomes mkdir -p $1 && cd $1 new-project. In a normal terminal (outside a function), $1 is empty, and new-project is left at the end. mkdir fails with missing operand: there is no folder name.

Meaning: An alias is only a text replacement. As soon as you need an argument or several steps, use a function.

⚡ Keep them permanently

Write:

source ~/.zshrc

Meaning: Put aliases and functions in ~/.bashrc (bash) or ~/.zshrc (zsh), the settings files the shell reads. Then reload the file with source. If they are not written in that file, aliases and functions are gone as soon as the terminal closes.

What you will learn