How to create custom bash commands?
4 years ago
0 comments comment

Ever since we have used computer, all might have heard of command line tool. Most commonly called bash. Command line is capable of taking input and outputting its results from and to computer. One of the features command line offers is it lets the users to create their own instructions. In this article we will take a tour through the steps on creating our own bash command.

Create custom command on Bash

This feature comes hand full when we are executing 2 or 3 everyday commands to do a single functionality. Let me make the picture more clear. I don't know how many would have created a virtual host. Here I'm considering the virtual host as the scenario. On creating virtual host, include the ip address along with the host name in /etc/hosts/ file. So let's get started.

[Hint: Assuming you are using Linux OS]

For performing with task one must execute the following tasks,

  • Go to /etc folder using
     cd /etc folder. 
  • Now open the hosts file to edit by sudo nano hosts.

So, why we do this all the time, when ever we want to add a newly created host to hosts file.

I have created a custom command for doing this by executing a single command. The steps to create these commands would be a mess, but I bet this is something very useful.

Lets start creating our own command,

  • Open up the terminal, type the command
    sudo nano

    Now a plain space must appear in your terminal.

  • Now enter the following
        #command to open /etc/hosts file
        sudo nano /etc/hosts

    Let me explain, The symbol you see is often referred as 'HashBang' which points to /bin/bash which means the command we've entered in this case sudo nano /etc/hosts runs using bash program. Second line is the comment for the instruction for later reference. Third line refers to the set of instructions to execute. .

  • Save the file with the name of your costum command you intend to make. Lets name it as 'ghost' in short of go to hosts file.
  • Now If you try to execute the command we have created, let's say ghost, you can notice that the 'command not found'.
  • As a next step, move the file to /usr/bin
    cp -i ghost /usr/bin

    Here we are copying ghost to /usr/bin folder

  • Now try to type ghost on bash, you we see 'permission denied' error.
  • From above we must get a clue, that the file we created is not executable. So lets move to give the file executing permission
    chmod +x ghost
  • Now make a run to execute your command on terminal. It must open up the hosts file in etc folder.

Bash is providing a universe of pre defined commands. Lets make it our own by putting some effort in customizing. By this way, we can create our own commands for everyday combination of commands we use in one step. Bash can do tasks faster than Graphical Interface. It also allow access to more commands and scripts.



on 11th December 2008 / by webmaster
Recently we had to provide support on a Red Hat Server where there were hundreds of users and the user directories organized into hierarchies. Normally we change to a users directory using cd ~username. We had some extra time on our hands and wanted to play with the /etc/passwd file and tried to use awk to do the same thing. The following one liner will do this the hard way :-) Linux Shell Scripting Bash Leave a reply Your email address will not be published. Required fields are marked * cherry (not verified) access_time 12 Nov 2019 - 15:51 alternatively: cd `(cat /etc/passwd|grep username|cut -d ':' -f 6` ;-) Add new comment

on 12th December 2009 / by webmaster
Asianet Dataline is an ISP in Kerala and they use a web browser based authentication system to lo Internet Shell Scripting Bash Asianet Leave a reply Your email address will not be published. Required fields are marked * Vivek Thomas (not verified) access_time 12 Nov 2019 - 19:33 Thank you very much for this. Works great. Btw I had to add a --no-check-certificate option in the following line for it get the login URL. :) asianet_conn_url=`wget --quiet --no-check-certificate -O -|grep 'action='|sed 's/\(.*action="\)\(.*\)">/\2/g'` webmaster access_time 12 Nov 2019 - 19:33 In reply to Thank you very much for this. by Vivek Thomas (not verified) Changes duly made in git repo and credit given. Anoop JITHIN (not verified) access_time 12 Nov 2019 - 19:33 In reply to Thanks by webmaster Sir, Where is this "Favorite Scripts Folder" How Can I Copy A Script ie In Text Form To A Folde? Is Favorite Script Folder an attachment Of Internet Explorer Browser ? Please Tell Me About "START UP PROCESS" Can You Plese Explain What Should I Do Step By Step Considering Me As A Beginer. Jimmy (not verified) access_time 12 Nov 2019 - 19:33 Hi, I see the script. Can I use this script in Windows? If yes, please tell me the complete procedure to use this script in windows using the browser IE. Thanks Jimmy Anonymous (not verified) access_time 12 Nov 2019 - 19:33 Thank you very much for this. Works great. But I cant logout after use Pagination Current page 1 Page 2 Page 3 Next page Next › Last page Last » Add new comment

on 14th December 2012 / by thomas.john
The default method of changing a Drupal theme using the graphical Admin interface is familiar to most Drupal users. However a Drupal user is limited to changing only one theme at a time using this method and it is also slightly time consuming. What if you want to enable more then one theme at a time? Drupal provides a quicker way to change the theme using the bash command line. Read on to know how to enable multiple Drupal themes using the bash command line Drupal Bash Drupal Themes Drupal Theming Drush Leave a reply Your email address will not be published. Required fields are marked *
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.