Blog

  • Introduction:

    In this blog, we will be learning how to create custom shortcut commands (aliases) in bash. The commands can be configured to execute multiple commands by just a single bash command. This comes really handy when one has to repeatedly do some task which involves multiple commands, for instance: creating builds, pushing code to git, etc

    For the sake of comprehensibility, in this blog, we will create a custom command to create a local GIT branch from an updated version of the master.

     

    Pre-requisites:

    • Operating System - Ubuntu.
    • GIT (latest stable version).
    • Editor (Nano/VS code/Gedit)

     

    Implementation:

    The custom commands are generally placed and defined in '.bashrc' file, this is basically a hidden file present in the home directory and thus is accessible from any location in the terminal. However, it is not an ideal practice to define all sort of custom usage commands in system files. For that reason. it is recommended to create a custom file ( ~/.custom_aliases) and place all our aliases in it.
    Note: It is necessary to load the aliases using 
    source ~/.custom_aliases, else the aliases just won't work.

     

    Step 1:- Creating the custom_aliases file.

    # create file
    touch ~/.custom_aliases

     

    Step 2:- Open file custom_aliases.

    # opens file
    gedit ~/.custom_aliases

     

     

    Step 3:- Enter custom function to execute multiple commands.

    # Multiple commands
    function createBranchFromMaster() {
        git checkout master
        git pull origin master
        git checkout -b "$1" master
    }

     

    Step 4:-   Changes update.

    # update file
    source ~/.custom_aliases

     

    Usage:

    # create branch
    createBranchFromMaster custom_branch
    
     

    Conclusion:

    For every task performed, time is a vital factor that impacts the performance and efficiency of a programmer. Hence, it is important to be efficient and creating such custom commands helps the cause greatly.  

     

    Thanks

     

Tags: DevOps