How to use shell aliases in Linux

What is an alias

An alias is a shorthand shell notation that allows you to customize and abbreviate commands. Aliases are available in all shells. A common syntax to define an alias on command line is as follows:

$ alias name=command_string

If the first word on the command line is an alias, the shell replaces that word with the text of the alias. The shell maintains a list of aliases that it searches when a command is entered. The following rules apply while creating an alias:

  • There can be no space on either side of the equal sign.
  • The command string must be quoted if it includes any options, metacharacters, or spaces.
  • Each command in a single alias must be separated with a semicolon.

Command Sequence

You can group several commands under a single alias name. Individual commands are separated by semicolons. For example:

$ alias info='uname -s; id; date'
Linux
uid=1001(user) gid=1001(user)
Tue Dec  5 15:11:58 UTC 2017

In the following example, an alias is created using a pipe (|) to direct the output of the ls -l command to the more command. When the new alias is invoked, a directory list appears.

$ alias ll='ls -l | more'
$ cd /usr/bin
$ ll
total 121528
-rwxr-xr-x. 1 root root       41496 Nov  5  2016 [
-rwxr-xr-x. 1 root root      107856 Aug  2 17:46 a2p
-rwxr-xr-x. 1 root root       52640 Oct 19 20:40 ab
-rwxr-xr-x. 1 root root       29112 Sep  6 16:47 addr2line
-rwxr-xr-x. 1 root root          29 Sep  6 16:25 alias
-rwxr-xr-x. 1 root root    12930120 Oct 25 18:22 amazon-ssm-agent
-rwxr-xr-x. 1 root root        7200 Jun 16  2016 animate
lrwxrwxrwx. 1 root root           6 Sep 29  2014 apropos -> whatis
-rwxr-xr-x. 1 root root       62680 Sep  6 16:47 ar
...
--More--

Predefined Aliases

The shell contains several predefined aliases. You can display these predefined aliases by using the alias command. For Example :

$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias info='uname -s; id; date'
alias l.='ls -d .* --color=auto'
alias ll='ls -l | more'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Note: The alias command also displays user-defined aliases.

User-Defined Aliases

User-defined aliases are defined by a user, usually to abbreviate or customize frequently used commands. For example, the history command is aliased as h using the alias command in the following code:

$ alias h=history
...
   46  cd /usr
   47  ls -lrt
   48  cd 
   49  cd /usr/bin
   50  ll
   51  clear
   52  alias
   53  alias h=history
   54  h

Using the rm, cp, and mv commands can inadvertently result in loss of data. As a precaution, you can alias these commands with the interactive option. For example, the rm command is aliased with the –i option as coded as follows:

$ alias rm='rm -i'
$ rm file1
rm: remove file1: (yes/no)? no

Similarly, creating a ‘cp -i‘ and ‘mv -i‘ alias ensures that the shell prompts you for confirmation before overwriting existing files.

Deactivating an Alias

You can deactivate an alias temporarily by placing a backslash (\) in front of the alias on the command line. For example, in the following code, the backslash prevents the shell from looking in the alias list. This allows the shell to run the original rm command to remove the file1 file.

$ rm file1
rm: remove file1 (yes/no)? no
$ \rm file1
$ ls file1
file1: No such file or directory

Or if the alias like h for history would give an error as below if you use the backslash.

$ \h
-bash: h: command not found

Removing an Alias

The unalias command removes aliases from the alias list.

$ unalias alias_name

For example, the h alias that was created earlier is removed using the unalias command.

$ unalias h
$ h
-bash: h: command not found
Related Post