Under Construction

The alias Mechanism

An alias allows you to replace a word with any character string. However, only the first word of a simple command is replaced. The following can be achieved with the help of aliases:

    • Abbreviation of a frequently used long command with a shorter word, e.g. “h” instead of “history“.
    • Adding a default option to a command, e.g. the “-F” option to the ls command.

However, you should not change the meaning or functionality of existing commands with an alias!

You can display which aliases are currently available using the builtin command alias without arguments:

[user01@aixe01 ~]$ alias
alias gt='gnome-terminal'
[user01@aixe01 ~]$

Note: You can also access the defined aliases via the bash variable BASH_ALIASES (array).

New aliases can be defined using the following syntax:

alias name=value ...

When using the ls command, many users find it useful to display the file type along with the file name (the “-F” option). To avoid having to type the “-F” option every time, you could define the following alias for ls:

[user01@aixe01 ~]$ alias ls="ls -F"
[user01@aixe01 ~]$

If the ls command is now called, the shell recognizes that an alias has been defined and replaces “ls” with the right side of the alias “ls -F“. Subsequent parts of the command remain unchanged. This executes the ls command with the “-F” option.

Note: After the replacement, “ls” reappears as the first word! However, “ls” is not recursively replaced again. The bash recognizes the recursion and doesn’t replace the word “ls” again.

Another command that is commonly used by many users is the history command. With 7 letters, it is quite a long command, so we replace it with the letter “h“:

[user01@aixe01 ~]$ alias h=history
[user01@aixe01 ~]$

As a final example of a meaningful alias, let’s define the alias “r” as “fc -s“:

[user01@aixe01 ~]$ alias r="fc -s"
[user01@aixe01 ~]$

The letter “r” is intended to stand for “repeat” here. The alias allows the last command to be executed again. Optionally, “old=new” can be specified as an argument, which results in the character string “old” being replaced by the character string “new” in the last command before the re-execution.

In order that the aliases are automatically available after logging in or starting a login bash, they must be defined in one of the bash startup files. We use the file ~/.bashrc:

[user01@aixe01 ~]$ cat ~/.bashrc

alias gt="gnome-terminal"
alias h='history'
alias ls='ls -F'
alias r='fc -s'

[user01@aixe01 ~]$

An alias can be removed with the unalias command for the current bash:

[user01@aixe01 ~]$ unalias h
[user01@aixe01 ~]$ h
-bash: h: command not found
[user01@aixe01 ~]$

By placing a backslash “\” in front of the first word of a command, the evaluation of aliases can be prevented:

[user01@aixe01 ~]$ \ls
IJ32630s4a.210813.epkg.Z    bin                         mod_hist                    stop.ksh
bash_history                etc                         sample.txt                  working_copy_of_sample.txt
bigfile                     keepopen.ksh                start.ksh
[user01@aixe01 ~]$ ls
IJ32630s4a.210813.epkg.Z     bin/                         mod_hist                     stop.ksh
bash_history                 etc/                         sample.txt                   working_copy_of_sample.txt
bigfile                      keepopen.ksh*                start.ksh
[user01@aixe01 ~]$

An alias only allows the first word to be replaced with a character string. Processing of arguments is not possible with aliases. If this is desired, one should use functions instead of aliases. These allow the processing of arbitrary arguments.