How to set alias in linux

How to

As a administrators we often use commands with options and arguments which are too long and we have to user them on regular basis. For that we canĀ use alias which will help us type shorter commands. This article will describe How to set alias in linux.

The alias utility shall create or redefine alias definitions or write the values of existing alias definitions to standard output. An aliasdefinition provides a string value that shall replace a command name when it is encountered.

The easiest way is set alias by command. This change is not permanent and after reboot or starting new session it will disappear.

system:~ #
system:~ # cdw
If 'cdw' is not a typo you can run the following command to lookup the package that contains the binary:
    command-not-found cdw
-bash: cdw: command not found
system:~ #
system:~ # alias cdw="cd /srv/www/htdocs/"
system:~ #
system:~ # cdw
system:/srv/www/htdocs #
system:/srv/www/htdocs #

If we want to create permanent executable aliases, we need to update ~/.bashrc file. To make it available to all users, alias must be defined in /etc/profile. All changes in ~/.bashrc or /etc/profile will be available hen new session will be started.

system:~ # cat .bashrc
alias cdw="cd /srv/www/htdocs/"
system:~ #

For removing alias we can use command unalias. The unalias utility shall remove the definition for each alias name specified. When we will use -a option we will Remove all alias definitions from the current shell execution environment. Alias will be removed even it is set in ~/.bashrc or /etc/profile.

system:~ #
system:~ # cat .bashrc
alias cdw="cd /srv/www/htdocs/"
system:~ #
system:~ #
system:~ # unalias -a
system:~ #
system:~ # cdw
If 'cdw' is not a typo you can run the following command to lookup the package that contains the binary:
    command-not-found cdw
-bash: cdw: command not found
system:~ #

Leave a Reply