Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท System Information

Bash alias command

Reference for the Bash alias and unalias builtins: define shortcuts, list them, escape or bypass an alias, and know when to use a function instead.

alias gives a short name to a longer command. Define alias ll='ls -l', and typing ll runs ls -l. The shell replaces the alias name with its text before it parses the line, which makes aliases pure text substitution: simple and fast, but limited.

Aliases belong to interactive use. Put your favourites in ~/.bashrc. For anything that needs arguments in the middle, conditionals or loops, write a shell function instead.

Terminal
alias [-p] [NAME[=VALUE] ...]
unalias [-a] NAME [NAME ...]

Try it

Bash Defining and using aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Escaping an alias and when a function is better

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
alias [-p] [name[=value] ...]
unalias [-a] name [name ...]
Name Description
alias (no arguments) Print all aliases in a reusable alias name='value' format.
alias -p The same, explicitly.
alias name Print the definition of one alias.
alias name=value Define or replace an alias. Quote the value if it contains spaces or special characters.
unalias name Remove an alias.
unalias -a Remove every alias in the current shell.

Returns: 0 normally. alias name returns 1 when the alias does not exist; unalias returns non-zero for an unknown name.

Errors and edge cases: alias a = b (with spaces around the equals sign) is read as three separate names and fails or prints nothing useful. Write alias a=b.

Usage notes

  • Where to define them. ~/.bashrc for personal aliases, ~/.bash_aliases if your .bashrc sources it (Debian and Ubuntu do), /etc/profile.d/*.sh for system-wide ones. Aliases are not inherited by child shells or scripts.
  • Scripts ignore aliases by default. shopt -s expand_aliases turns them on, but a function is almost always the better choice (see shopt).
  • Bypassing an alias. Use \command, 'command', command command or the full path (/bin/ls). This matters when an alias like rm='rm -i' would block a script.
  • Trailing space. If an alias value ends in a space, the next word is also checked for aliases: alias sudo='sudo ' lets sudo ll expand ll.
  • Quoting. Single quotes keep $VAR and backticks unexpanded until the alias is used; double quotes expand them at definition time. Prefer single quotes.
  • Alias vs function vs script. Aliases cannot take a parameter in the middle, cannot have local variables and are read before parsing. Functions solve all of that. Use type to see what a name currently is.
  • Overriding commands. alias ls='ls --color=auto' is common and harmless, but alias rm='rm -rf' is dangerous. Be careful with aliases that change defaults of destructive commands.
  • Temporary session. Aliases defined at a prompt vanish when the shell exits.