How to Make a Bash Alias
An alias is a command in shells that allows a word to be replaced by another string. It is mostly used to shorten a system command or provide default parameters for a frequently used command.
It’s similar to a shortcut command, with the same functionality as writing the entire command.
Create Alias in Bash
Let’s look at an example.
$ alias update="sudo apt-get update"
We created an alias update
, which is a shortcut for sudo apt-get update
. Now when we run update
, it works the same as sudo apt-get update
.
$ update
Output:
[sudo] password for username:
Get:1 https://typora.io/linux ./ InRelease [793 B]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:3 http://np.archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease
Ign:5 http://linux.dropbox.com/ubuntu disco InRelease
Get:6 http://np.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Hit:7 http://linux.dropbox.com/ubuntu disco Release
Ign:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Hit:10 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release
Get:12 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [35.7 kB]
Get:13 http://np.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:14 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [66.3 kB]
Get:15 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [278 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,468 B]
Get:17 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [363 kB]
Get:18 http://np.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:19 http://np.archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,996 B]
Get:20 http://np.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [11.3 kB]
Fetched 1,102 kB in 9s (120 kB/s)
Reading package lists... Done
To list all the aliases, run the below command.
$ alias
Output:
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias echo='show'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias show='echo'
alias update='sudo apt-get update'
The newly created alias update
is also in the list with all the other aliases.
Create Bash Alias That Takes Parameters
The Bash alias does not accept parameters directly. We have to write a function because the function mechanism is more adaptable and provides the same functionality as before.
Although an alias does not take parameters, a function can be invoked in the same way that an alias can. Let’s look at an example.
$ alias wargs='f(){ echo first "$@" last; unset -f f; }; f'
$ wargs a b c
In the above example, a temporary function f
is created. The arguments are sent when f
is called at the end.
The unset -f
option eliminates the function definition while the alias is performed, ensuring it does not linger afterward.
Output:
first a b c last
Remove Alias in Bash
We can remove the previously created alias update
by using the unalias
command.
$ unalias update
Let’s check the list of all the aliases again.
$ alias
Output:
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias echo='show'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias show='echo'
alias wargs='f(){ echo first "$@" last; unset -f f; }; f'
As seen in the output, alias update
is removed from the alias list.