Last updated:

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.

The Bash 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 alias and unalias builtin commands.

How to set a Bash Alias?

You can define a new alias by using the Bash command alias [alias_name]="[command]".

A Bash alias name cannot contain the characters /, $, ``, = and any of the shell metacharacters or quoting characters.

The Bash 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 .bashrc or .bash_aliases file, make sure to reload your Linux shell 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="ls -l", then another alias as alias la="ls -a", the second alias will not expand to ls -la and will only be ls -a.

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 shopt expand_aliases.

[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'

Alternatively, in Bash, you can also use the environment variable $BASH_ALIASES to list all the existing aliases. The $BASH_ALIASES variable is a bash associative array that contains the list of aliases as created by the Bash alias builtin. $BASH_ALIASES will lose its special properties when being unset.

You can use a bash for loop to iterate over the alias names using ${!BASH_ALIASES[@]} and return the alias definition using ${BASH_ALIASES[<alias_name>]}.

[me@linux ~$]$ for alias_key in ${!BASH_ALIASES[@]}; do echo "$alias_key = ${BASH_ALIASES[$alias_key]}"; done
dir = ls -color=auto -format=vertical
ls = ls -color=auto

⚠️ Elements can be directly added to the $BASH_ALIASES array to create new aliases. This is not good practice and the use of the alias builtin should be prefered. Removing an alias from the associative array does not cause aliases to be removed from the alias list. The unalias builtin must be used.

[me@mac ~]$ echo ${#BASH_ALIASES[@]}
[me@mac ~]$ BASH_ALIASES[ls]="ls -a"
[me@mac ~]$ echo ${#BASH_ALIASES[@]}
[me@mac ~]$ echo ${!BASH_ALIASES[@]}
[me@mac ~]$ echo ${BASH_ALIASES[ls]}
ls -a
[me@mac ~]$ alias ls
alias ls='ls -a'
[me@mac ~]$ unset BASH_ALIASES[ls]
[me@mac ~]$ echo ${BASH_ALIASES[ls]}
ls -a
[me@mac ~]$ unalias ls
[me@mac ~]$ echo ${BASH_ALIASES[ls]}

[me@mac ~]$ alias ls
bash: alias: ls: not found

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 -a option.

# 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?