Monday, 16 March 2015

Tips: Bash' Alias Command

An alias is nothing but a shortcut to a command. The alias command allows a user to launch any command by entering a single word. Typing alias will display a list of all defined aliases. You can add your own aliases by editing the ~/.bashrc file.

The general syntax for the alias command for the bash shell is as follows.

List Aliases

Type the following:
alias
Example output:
alias ..='cd ..'
alias apt-get='sudo apt-get'
...

Create an alias

To create an alias, use the following syntax:
alias name=value
alias name='command'
alias name='comand arg1 arg2'
alias name='/path/to/script'
alias name='/path/to/script.pl arg1'
Int his example, we create an alias('c') for the commonly used clear command, which clears the screen, by typing the following command.
alias c='clear'
Then, in order to clear the screen, instead of typing clear, we only have to type the letter c.

Disable an Alias

An alias can be temporarily disabled by using the following syntax:
## path/to/full/command
/usr/bin/clear
## call alias with backslash
\c

Remove an Alias

To permanently remove an alias we use the unalias command:
unalias aliasname

Make a permanent alias

Our alias c only remains in effect during our current login session. One we log out or reboot the system, the alias c will be destroyed. To avoid this and make the alias permanent, add the alias to you ~/.bashrc file.
vi ~/.bashrc
And now,
alias c='clear'
Save and close the file and you can now use your alias. If you want to make an alias that all users can you use, put the alias in the /etc/bashrc file.

No comments:

Post a Comment