How to Set alias of a Command in Linux
An alias is a shorthand notation in the Bash shell to enable you to customize and abbreviate UNIX commands. An alias is defined by using the alias command.
Command Format
The alias command syntax is as follows:
$ alias name=command_string
For example:
$ alias dir=’ls -lF’
The shell maintains a list of aliases that it searches when a command is entered. If the first word on the command line is an alias, the shell replaces that word with the text of the alias. When an alias is created, the following rules apply:
- 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.
Predefined Shell Aliases
Many shells contains several predefined aliases, which you can display by using the alias command. User-defined aliases are also displayed. For example in Korn shell you may find some of the below aliases pre-defined.
$ alias
autoload='typeset -fu'
command='command '
functions='typeset -f'
history='fc -l'
integer='typeset -i'
local=typeset
nohup='nohup '
r='fc -e -'
stop='kill -STOP'
suspend='kill -STOP $$'
User-Defined Aliases
Aliases are commonly used to abbreviate or customize frequently used commands. For example:
$ alias h=history
$ h
278 cat /etc/passwd
279 pwd
280 cp /etc/passwd /tmp
281 ls ~
282 alias h=history
283 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:
$ alias rm=’rm -i’
$ rm dat1
rm: remove dat1: (yes/no)? no
$
By creating a cp -i and mv -i alias, the shell prompts you before overwriting existing files. You can deactivate an alias temporarily by placing a backslash (\) in front of the alias on the command line. The backslash prevents the shell from looking in the alias list, thereby causing it to perform the original rm command.
For example:
$ rm file1
rm: remove file1 (yes/no)? no
$
$ rm \file1
$ ls file1
file1: No such file or directory
Command Sequences
You can group several commands together under a single alias name. Individual commands are separated by semicolons. For example:
$ alias info=’uname -a; id; date’
$ info
SunOS host1 5.8 Generic sun4u sparc SUNW,Ultra-5_10uid=102(user2)
gid=10(staff)Fri Jun 30 15:22:47 MST 2000
$
In the next 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 listing appears. If the listing is longer than the available space on the screen, the ‘–More–’ line item appears at the end of the list, indicating that additional directory contents are displayed on the subsequent screen. For example:
$ alias ll=’ls -l | more’
$ cd /usr
$ ll
total 136
drwxrwxr-x 2 root bin 1024 May 13 18:33 4lib
drwx------ 8 root bin 512 May 13 18:14 aset
drwxrwxr-x 2 root bin 7168 May 13 18:23 bin
drwxr-xr-x 4 bin bin 512 May 13 18:13 ccs
drwxrwxr-x 5 root bin 512 May 13 18:28 demo
--More-
Removing Aliases
Use the unalias command to remove aliases from the alias list. The unalias command syntax is as follows:
$ unalias alias_name
For example:
$ unalias h
$ h
ksh: h: not found