Shell builtins · bash builtin
alias
Define a short name that the shell replaces with a longer command line, or list the aliases already defined.
alias [-p] [NAME[=VALUE] ...]
An alias is a text substitution done by the shell's parser: when the first word of a simple
command matches an alias name, the shell swaps in the alias value before doing anything else.
alias ll='ls -l' makes ll data run ls -l data. Aliases are meant for interactive
typing; they live in ~/.bashrc and save keystrokes.
Because the substitution happens while the line is being read, two rules follow. An alias
is only recognized in the command position (the first word), and an alias defined on a line
takes effect only for lines read after it. Non-interactive shells, which includes every
script and every bash -c, ignore aliases completely unless you run shopt -s
expand_aliases. The examples on this page do exactly that.
For anything with arguments in the middle, logic, or use in scripts, write a function instead
(ll() { ls -l "$@"; }). Functions work everywhere, take arguments anywhere, and do not
depend on parse timing. To remove an alias use unalias NAME; to bypass one for a single
command, prefix it with a backslash (\ls) or use command.
Examples
Define and use an alias
shopt -s expand_aliases
alias lt='ls -t'
lt data | head -n 3
users.csv poem.txt windows.txt
lt data is rewritten to ls -t data before it runs. The shopt line is needed only
because examples run non-interactively; in your terminal aliases are on by default.
Aliases do nothing in scripts by default
alias greet='echo hello'
greet
/bin/bash: line 2: greet: command not found
A non-interactive bash (a script, bash -c, cron) defines the alias but never expands
it, so greet is looked up as a command and is not found. This is the reason an alias
from your .bashrc "does not work" in a script.
An alias is not available on the line that defines it
shopt -s expand_aliases
alias hi='echo hi there'; hi
hi
/bin/bash: line 2: hi: command not found hi there
The whole first line is parsed before alias runs, so hi on that line is looked up as
a command and fails with status 127. On the next line, read after the definition, the
alias expands. The same applies inside a function body or an if block: the whole block
is parsed at once.
List the defined aliases
alias ll='ls -l' la='ls -A' gs='git status'
alias
alias gs='git status' alias la='ls -A' alias ll='ls -l'
With no arguments, alias prints every alias in a form that could be pasted back into
the shell, sorted by name. alias -p does the same. Several aliases can be defined in
one command.
Show one alias
alias ll='ls -l'
alias ll
alias nope
alias ll='ls -l' /bin/bash: line 3: alias: nope: not found
Given only a name, alias prints that definition. An unknown name is an error, so the
exit status is 1.
Arguments always go at the end
shopt -s expand_aliases
alias count='wc -l'
count todo.txt notes.txt
5 todo.txt 6 notes.txt 11 total
Whatever follows the alias name is appended after the replacement text. There is no way to put an argument in the middle of an alias; that is the point where a function is the right tool.
Use a function when you need arguments
errors() { grep -c "ERROR" "$@"; }
errors logs/app.log
errors -h logs/app.log logs/error.log
2 2 0
A function receives its arguments in $@ and can place them anywhere. It works in
scripts without any shopt, and options such as -h pass straight through.
Bypass an alias for one command
shopt -s expand_aliases
alias ls='ls -F'
ls scripts
\ls scripts
command ls scripts
backup.sh* greet.sh* backup.sh greet.sh backup.sh greet.sh
With the alias, ls appends * to executables. A backslash on the command name, or
quoting any part of it, prevents alias lookup, as does running it through command.
Make the next word alias-expanded too with a trailing space
shopt -s expand_aliases
alias ll='ls -l'
alias run='time -p '
run ll -d data
drwxrwxr-x 2 dev dev 13 Oct 25 01:33 data real 0.00 user 0.00 sys 0.00
Only the first word is normally checked for aliases. When an alias value ends in a space,
the word after it is checked too, so ll expands after run. This is why people define
alias sudo='sudo '.
Remove an alias
alias ll='ls -l' la='ls -A'
unalias ll
alias
unalias -a
alias | wc -l
alias la='ls -A' 0
unalias NAME removes one alias; unalias -a removes them all. Useful at the top of a
sourced file that wants predictable commands.
Common pitfalls
Expecting aliases in scripts. A non-interactive bash ignores them; even with
shopt -s expand_aliasesthey apply only to lines read after the definition. Write a function.Defining an alias and using it on the same line, or inside the same function or compound command: it is not expanded until the next separately parsed command.
Double quotes expand variables when the alias is defined:
alias here="cd $PWD"freezes the current directory. Single quotes defer expansion to each use.An alias that shadows a command, such as
alias rm='rm -i', trains you to rely on a safety net that is missing on other machines and in scripts.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
Aliases exist in every POSIX shell with the same syntax. zsh expands aliases in scripts by default and also has global aliases (
alias -g); dash honors them in scripts without any option.
Options worth knowing
A selection, described in our own words. For the full list, see bash(1), SHELL BUILTIN COMMANDS.
| Option | What it does |
|---|---|
-p | Print all aliases in reusable alias NAME=VALUE form. |
Related
typeShow how the shell would interpret a name, as an alias, keyword, function, builtin or file on PATH.commandRun a command while skipping functions and aliases of the same name, or check whether a command exists with -v.unsetRemove shell variables, array elements or functions, so they no longer exist rather than being empty.sourceRun the commands in a file inside the current shell, so its variables, functions and directory changes stay in effect.- FunctionsDefining and calling bash functions, arguments, local variables, return status versus output, namerefs, and sourcing a function library.
- Running scriptsThe four ways to start a script (./script, bash script, PATH, source), what each one changes, and the errors you get when one goes wrong.