A Quick Guide on How to Use Bash Alias
A Bash alias act as a shortcut for command lines. It is a convenient way to simplify the use of very long and repetitive commands that you use frequently.
alias command allows a string to be substituted when it is used as the first word of a command line. The shell maintains a list of aliases that may be set and unset with the Bash
unalias builtin commands.
How to set a Bash Alias?
You can define a new alias by using the Bash command
A Bash alias name cannot contain the characters
= and any of the shell metacharacters or quoting characters.
alias command can be used in your
.bashrc file to define all the aliases you want. In some cases, you may want to use the
.bash_aliases file which is generally sourced from your
.bashrc file. When changing your
.bash_aliases file make sure to reload your linux shell in order for the changes to take effect in your current terminal session.
Below is a common list of Bash aliases:
# .bashrc example alias ls="ls -color=auto" alias dir="ls -color=auto -format=vertical" alias vdir="ls -color=auto -format=long" alias ll="ls -l" alias la="ls -A" alias l="ls -CF"
Note that Bash will not expand aliases recursively. For example, if you declare an alias
alias="ls -l", then another alias as
alias la="ls -a", the second alias will not expand to
ls -la and will only be
Also, Bash aliases are not expanded when your shell isn't interactive, unless the
expand_aliases shell option is set using
shopt -s. You can check your current setting (on or off) by using
[me@linux ~]$ shopt expand_aliases expand_aliases on [me@linux ~]$ shopt -s expand_aliases
If arguments are needed in an alias, you would need to use a bash function instead. Generally, shell functions are preferred over aliases.
How to list existing Bash Alias?
You can use the
alias -p command to list all the alias currently defined.
[me@linux ~]$ alias -p alias dir='ls -color=auto -format=vertical' alias ls='ls -color=auto'
How to unset (delete) a Bash Alias?
You can unset (or delete) an existing Bash
alias by using the Bash
unalias builtin command. All the existing aliases would be removed when using the
# unset "ll" alias [me@linux ~]$ unalias ll # unset all aliases [me@linux ~]$ unalias -a
👉 Read more about Bash aliases with my post on How To Find Quickly A Bash Command Type?