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

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 -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...
The Complete How To Guide of Bash Functions
Learn how to write shell scripts with bash functions. This guide includes examples and best practices on how to define, call, and debug functions in bash.
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 do Bash Loops?
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.