Linux alias command tutorial with example

What is Alias

Alias is a command(User Chosen e.g. JBT) which is used to launch any system specific(e.g. ls) command or group of command inclusive of any options,arguments or redirection. In simple word it is(Alias command) providing you option to use simple string for any complex command(Shortcut of a complex command).

Example

Linux Command :  ls -lrt

User Command : JBT

alias JBT=’ls -lrt’

Now you can use JBT as a command which will work the same as “ls -lrt”


How to create alias

Alias can be created using alias command. General syntax of creating alias is as below.

alias <user_defined_alias_name>=”<system_specific_command> <options> <argument>”

Note*: There should not be any space on either side of equal sign(=)


How to check existing alias

Now you want to create an alias but before that you want to check if any alias is already created with given name or not. To check all aliases created in given shell you can execute alias command  without any argument and it will show you all aliases created(User created along with some system created).

As you can see here, ls also an alias which is created by system and available to all users.


How to use Core Command and not Alias

As you can see above ls, which we use is already an alias. So when ever we execute ls we are actually executing alias with name ls(ls –color=auto) and not the core command. So what if we want to execute the core command ls and not alias ls.

Alias can be disabled(Temporarily) by using backslash(\) before command(No space between command and backslash).

 


How to create permanent Alias

Till now we have created all aliases in session only and it will not available once you logout. So next thing would be to create permanent alias. Permanent alias can be created by writing the same command in .bashrc file in user home directory.(I am using UBUNTU, file might be different for your system). .bashrc file is used to read at login time so changes done in this file will take effect every time you login and hence this alias will be permanent.

Note*: Changes done in this way will be applicable to given user only. If you want to create alias which is available to all user then you need to write alias command in /etc/bash.bashrc file.


How to remove alias

To remove any alias you can use unalias command. 

Note*: This command will not remove any permanent alias created in bash file. This remove will be applicable to current active session only.


Bullet Point

  • Alias name can contain any valid input except equal sign(=).
  • To enclose commands single quotes() or double quotes() can be used.
  • Single characters can also be used as alias.
  • An alias name can be same as core command name.
  • To use core command instead of alias name use backslash(\).
  • Alias can be created to call other alias.
  • Multiple command can be used while defining alias.
  • Proper arguments can also be used with alias.
  • Creating a new alias with same name will override the old alias.

Leave A Comment

Your email address will not be published. Required fields are marked *