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.
alias [-p] [NAME[=VALUE] ...] unalias [-a] NAME [NAME ...]
Try it
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.
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
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.
~/.bashrcfor personal aliases,~/.bash_aliasesif your.bashrcsources it (Debian and Ubuntu do),/etc/profile.d/*.shfor system-wide ones. Aliases are not inherited by child shells or scripts. - Scripts ignore aliases by default.
shopt -s expand_aliasesturns them on, but a function is almost always the better choice (seeshopt). - Bypassing an alias. Use
\command,'command',command commandor the full path (/bin/ls). This matters when an alias likerm='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 'letssudo llexpandll. - Quoting. Single quotes keep
$VARand 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
typeto see what a name currently is. - Overriding commands.
alias ls='ls --color=auto'is common and harmless, butalias 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.
Related
type,unset,shopt,source- Lessons: Shell Basics, Functions, Environment