Open In App

Bash Scripting – Working of Alias

Improve
Improve
Like Article
Like
Save
Share
Report

BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible.

What is a BASH Alias?

A BASH Alias is a map of commands with the sets of commands or functions that can be used as a shortcut in the command line for a BASH environment. Bash Alias allows to aggregate multiple functions into a single command and also it avoids repetitive or large commands into a simple shortcut command. BASH Alias is a map of shortcut command with the sets of commands which are stored in a file called either .bash_profile or .bashrc with a specific syntax. The aliases can also be combined with the BASH syntax and semantics to make the shortcuts more customizable and flexible.

Working of an Alias

The alias keyword replaces the command with the string which might be sets of commands or functions. The alias is defined in the ~/.bashrc or ~/.bash_profile. These files are loaded in the shell environment and thus the commands listed in the alias are also been loaded and ready to be executed. Thus, the alias work with the BASH configuration.

After the aliases or command maps have been loaded, the user can interact and enter the commands. If the command in the alias is executed the BASH interpreter replaces the command alias with the string of commands and hence the desired functioning of the alias or command is achieved.

We’ll see how to create a BASH Alias in the next section.

How to create an Alias?

To create an alias, we need to create a file called bash_profile or bashrc. This file needs to be in the root directory and should be hidden to avoid mishandling of its contents.  Inside of this file, we create an alias which is basically a map of commands and the sets of commands/functions to be executed as a shortcut.

Creating .bashrc or .bash_profile

Firstly, you can locate to your root directory, open up a terminal or command prompt and type in the following command:

For Linux/macOS:

cd ~

For Windows:

If using git bash terminal or Powershell

cd ~

You should be now into so-called the root directory of your environment.

After moving to the root directory, you need to create a file called .bashrc or .bash_profile in the root directory itself. You can create the file however you create the file as per your preferences but to keep a standard way for most of the operating systems the following command will work:

echo >> .bash_profile

OR

echo >> .bashrc

This will create a file if not present but won’t do anything if the file is already created and has content. 

If you don’t like to move into the root directory then create and write to the file, you can access the root directory with the “~/“. You can create the file being in any other directory or Path in your OS. The following command creates the file without changing your current directory.

echo >> ~/.bash_profile

Creating a function

We can create an independent function in our bashrc or other bash scripts and further link those with a shortcut command by calling them. To create a function in BASH, we can use a simple syntax as follows:

function x()
{
   # statements
}

OR

x()
{
    # statements
}

We can then, write the logic for the command in this function block. For this example, we can create a function to create a directory and cd or move into that directory. Hence, we can write the code inside the function block like so:

function x()
{
   mkdir $1
   cd $1
}

 The $1 is a positional parameter to take arguments from the command line and parse it to a function or in a script as a variable. You can learn about them in this article. Simply put, they make the script or the alias more dynamic and allow easier user input directly from the command line.

Creating the BASH alias

Now, we have the bashrc or bash_profile created, we can start with creating aliases. To create an alias we will require three things, the alias keyword to state that the following sentence will create an alias, the command or a shorthand word to be used for running the last piece i.e. the commands which are to be executed. The commands inside an alias need to be executable from the Shell/ BASH environment.

The syntax of an alias is as follows:

alias command="sets of commands/functions"

As said earlier, we require three pieces of information i.e. the alias keyword, the shorthand command, and the sets of commands or functions to be executed.

How to set a Bash Alias?

We can access the value of the last command as a return type by using the $? outside the function after the function call. But to just execute commands we can simply call the functions

Let’s take an example to create an alias, suppose we want to create a directory and move into it, We will have to run two commands i.e. mkdir and cd. This can be a scenario to create an alias. We can chain up commands into a function or alias commands to avoid typing all commands. 

The alias would look like this:

alias md='x(){ mkdir "$1"; cd "$1"; }; x'

The alias keyword indicates the BASH interpreter to load the map variable as a command.  The command is named “md” which is mapped to a function “x”. The function x creates the directory with the name passed to the md command and changes the directory into it. The function is defined and called in the end so as to keep it simple and straightforward.

The other way to do the same trick is to create a function independently elsewhere and call from the alias. The following alias dies the same thing but written in a different way:

alias md='x'
x(){
    mkdir $1
    cd $1
}

This looks a lot readable and clean. This can also be used to add BASH syntax and semantics like loops, conditions, and other programmatic approaches to make the alias more customizable and flexible.

Difference between single and double quotes

The single and double quotes used in the alias statements have some importance and need to be used as per requirement. We use double quotes to expand the value of the variable in BASH i.e to actually parse in the value of the variable name. If we just use single quotation marks, the variable’s value is not displayed and just the variable-name is parsed as it is.

For example in the statement :

#!/bin/usr/env bash
p=125

echo "The value of variable p is $p"

echo 'The value of variable p is $p'

In this example, the value of the variable p is only expanded or parsed in the command if we include it inside the double quotation marks. This also applies when working with an alias in the bashrc or bash_profile files, we need to use double quotations to expand the value of the variable.

Structure of an Alias

After creating an alias we can understand the structure of an alias in a bit of depth.  As discussed earlier, we have three parts in creating an alias:

alias keyword

The alias keyword in BASH replaces a string with another string. The shorthand command is the string that will be used instead of the string on the right-hand side in the command. Hence the alias keyword acts as an abbreviation for a string which is a set of commands or functions.

command

The command is the shorthand that will be used in the terminal. We can use any command or string avoiding the actual commands like cd, mkdir, rmdir, touch, echo, etc which might conflict with them and result in undesired behavior. 

commands/functions

These are the sets of commands that will be executed when the command in the alias is entered. This can be BASH functions, shell commands, or any other sets of commands that are compatible with the Shell environment. 


Last Updated : 11 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads