How to Create and Use Alias Command in Linux

Rashmi Bhardwaj | Blog,Programming & Software
Google ADs

Using CLI or command line at times is cumbersome even for seasoned hardcore system administrators, when we want work to get done faster, we spend less time thinking what to put in command line parameters. Use of alias command  is quite common and a useful feature in Linux. Using alias help to reduce effort involved in typing a lot of command details, especially the ones hard to remember or have lengthy strings of options to be fed in a specific order to get desired output.   

In today’s lesson we will cover how to create and use alias in Linux, types of alias available in Linux.

Create and use Alias command in Linux 

Alias are custom shortcuts used to represent a command or a set of commands they can be of several types before we actually jump into seeing how aliases are created and used let us look in brief what alias types are there with examples.

Google ADs

Some aliases may be complete commands, some may include in a command line several parameters such as file name, username, or any other argument which you will supply when you use them. Alias command let create shortcuts for commands and make them easier to execute them. Some of the prerequisites of using alias in Linux is as under:

  • System running Linux distribution
  • An account with SUDO privileges
  • Access to terminal window or command line 
  • Text editor such as Vim or nano 

Alias has following syntax

alias [option] [name]='[value]’

  • Alias invokes alias command
  • Option allows command to list all current aliases
  • Name define new name for alias (Can’t use ‘alias’ and ‘unalias’ as name string)
  • Value specifies command alias references. It may include operations, arguments, and variables

Alias can be created temporarily or permanently. temporarily alias is created on command line (CLI) while permanent alias is defined in shell configuration file depending of shell being used

Bash shell: ~/.bashrc

Zsh shell: ~ /.zshrc

Fish shell: ~ /.config/fish/config.fish

There are several options to create alias 

  • Creating alias for a ‘file’ command as ‘fi
  • Creating alias for ‘ls-l’ command as ‘ll’
  • Creating alias with two arguments
  • Creating alias for a path 

Some aliases may also be abbreviations. 

The syntax of alias start with word ‘alias’ followed by name you want to give to alias for its identification 

$alias aliasname= ‘some command to use’

$alias Hello = ‘echo hello everyone’

To execute a command having ‘ you can use backlash which tells bash not to interpret it as delimiter but an additional character. e.g. 

$alias today= “echo Today\’s ‘date+%T’”

$today 

$ Today’s 09:41:20 

Sometimes alias can be used with variables e.g.

$alias hello= ‘echo Hello, $USR’ 

$hello

Conditional alias is used where you want to execute logic-based commands such as if 

$alias warning=’if [“$USR”! = “test”]; then echo Today\’s ‘date+%T; fi’

$warning

$ Today’s 09:41:20 

It is possible to create aliases which can handle a string of arguments which you provide on command line to use them. Some examples are: 

$alias Show=’f () {echo “$@” in that order; unset -f 

$show Linux is fun to play

The final “f” in each alias is the call to the alias after it is defined in the portion that begins with “f ()”.

You can share your aliases in the shell configuration file so whenever you login it will be available for use. Many shells support aliases not just bash like zsh uses a .zshev file. 

To remove an alias, use the unalias command with the below syntax 

Unalias [name] 

Adding the option -a allows to remove all aliases

$unalias -a 

Continue Reading:

What is Linux Lite?

Introduction to Linux Operating System

Are you Preparing for your Next Job Interview?

If you want to learn more about Linux, then check our e-book on Linux Interview Questions and Answers in easy to understand PDF Format explained with relevant Diagrams (where required) for better ease of understanding.

ABOUT THE AUTHOR


Leave a Comment

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

Shopping Cart