A Quick Guide on How to Use Bash Alias

  • HOME
  • >
  • BASH
  • >
  • A Quick Guide on How to Use Bash Alias
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 -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?

Related bash posts that you may like...
5 Simple Steps On How To Debug a Bash Shell Script
Learn how to quickly debug scripts in Bash with those 5 simple steps that use some of the Bash shell special properties.
A Complete Guide to the Bash Environment Variables
A complete guide on the Linux Bash environment variables with details on how to set, unset, and use the specials shell variables or define custom environment variables.
How To Make A Custom Bash Shell Prompt
Learn how to customize your shell prompt and the specificity of the Bash Prompt variables PROMPT_COMMAND, PROMPT_DIRTRIM, PS0, PS1, PS2, PS3, and PS4.
The Most Useful Bash Shortcuts That You Will Want To Use
A detailed review of the most useful Terminal and Bash shortcuts to improve your productivity when typing command-lines and how to customize the ones you don't like.