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'
- The form is
alias name='command'. No spaces around the=, and the command is wrapped in quotes. - When
llis the first word you type, the shell replaces it withls -la, then runs it. Any other words after it stay:ll notesbecomesls -la notes. - A word that isn't an alias, like
pwd, runs as it is.
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
type nametells you whether a name is an alias, a function, a shell builtin or a program. If the answer isbash: type: name: not found, that name is still free to use.aliaswith nothing after it lists every alias, in alphabetical order.unalias llremoves one alias, herell.
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
}
mkcd() {opens a function calledmkcd, and}closes it. The steps go between the two, one step per line, indented two spaces.$1inside a function is the first argument given when the function is called (run by typing its name), just like$1in a script.&&means "run the command on the right only if the command on the left succeeded". Ifmkdirfails,cddoesn't run.mkdir -pmakes the folder together with any parent folders that don't exist yet, and doesn't complain if the folder is already there. Somkcd projects/newmakesprojectsandprojects/new, then moves straight intoprojects/new.deployneeds no argument: its three steps run in order every time you typedeploy. A function with three steps takes five lines: the opening, three steps and the closing.
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
- Spaces around
=.alias ll = 'ls -la'makes no alias. Instead, bash looks up three aliases that don't exist:bash: alias: ll: not found,bash: alias: =: not found,bash: alias: ls -la: not found. - Using
$1in an alias. An alias can't fill$1. As soon as you need an argument in the middle of a command, use a function. $1without quotes in a function. In bash,$1without quotes is split at spaces. So ifmkcdusesmkdir -p $1andcd $1, thenmkcd "final project"makes two folders,finalandproject, andcdalso gets two arguments. Bash on Linux then refuses withtoo many arguments, while bash 3.2, the bash that comes with macOS, quietly moves intofinal. Zsh does not split$1, but"$1"is right in both shells, so always write"$1".- A name that hides another command.
alias ls='ls -la'or a function calledtestquietly replaces the real command with the same name. Check first withtype name.
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.