Working with Bash Aliases (Alias/Unalias)

  • HOME
  • >
  • BASH
  • >
  • Working with Bash Aliases (Alias/Unalias)
Last Updated: 
subscribe to my newsletter

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

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.

# unset "ll" alias  
[me@linux ~]$ unalias ll

# unset all aliases  
[me@linux ~]$ unalias -a

👉 Read more about Bash aliases with the post to Find How a Bash Command will be Interpreted

Related bash posts that you may like...
How To Create Simple Menu with the Shell Select Loop?
The select loop is not a regular shell loop. It can be used in Bash to generate a simple menu from which a user can select numbered options.
What is the Right Way to Loop in Bash?
Looping over a list of numbers or words is a building block in shell scripts. Learn how to write Bash loops, including for loop, while loop, and until loop.
What is the Best Way to Count Files in a Directory?
Learn how to count the number of files in a directory using the Linux command line ls, find, and a native bash shell solution with globs and arrays.
5 Mistakes To Avoid For Writing High-Quality Bash Comments
Adding comments in your Bash scripts is necessary to ensure maintainability over time. This post covers 5 Bash comments mistakes to avoid in your shell scripts.