How to Build Your Own Commands in Linux?
Linux is one of the most widely used open-source operating systems which supports both GUI as well as CLI. It has been widely used all over the world since its first distribution launched on September 17, 1991, by Linus Torvalds. It is widely known for its command-line operations. We have been using several commands in Linux like date command, mkdir command and many more. You can read more from Linux Commands.
There are more than thousands of commands we use in Linux but we never thought of creating our commands. Building your commands can be a really fun and interesting way of learning and experimenting with your knowledge. While building new things you will come across many new concepts and you will get to learn many new things.
Here, we are going to build a new command for Linux distros which is not present in any of the distributions of Linux. We are going to build a real-time command for showing time at the terminal continuously without disturbing our terminal to perform other tasks. Let’s just have a look at it.
To build something like this we need to have a good knowledge of Linux commands with their attributes and how to integrate Linux knowledge with scripting. To know all the attributes of command we can use, man command
Moving further there is a command named echo which is used to display the output in the screen. For basic uses of echo command with examples click here. Apart from the basics, this command can also be used for displaying outputs of other commands. For example,
Here in this example, you can observe that when we run date command just within double quotes (“date”), echo took it as a string and printed it but when we run it within backquotes (“`date`”) it gave the output of date command. So we can also use echo for displaying the output of other commands.
We are already familiar with attributes of echo command like -n, -e, etc. If not, then please click on the above link and read the article for the detailed use of echo command. Now if we want just to display the time using date command we can use “date +%T”. For more details, you can use man command.
Now we know that in echo command for printing in next line we use \n, for backspace \b, etc. To work with these attributes we use -e and for printing output in same line we use -n. Suppose we need to print time in same line but we don’t want to show seconds, we can use echo -n -e “`date +%T` \b\b\b”.
As in this example, we can see that when we used only -n attribute the output was printed in the same line, and also when we used \b with -n it just printed it as a string. When we used only -e the output was on the next line. But when we used -n and -e together with \b, the seconds were not visible. In simple terms, the last three digits ie, “:12” were not printed.
Now we need to think, how we can use these skills in such a way that every time date should be printed with seconds also. If somehow we make it run continuously then every second we will get a new time and at the same time, it will be deleted due to backspace (\b) which we used. To make it run this command continuously we need to use scripting and now we are going to integrate our Linux knowledge with scripting.
For any command to run using scripting, a very basic syntax is followed. We just need to write the commands in a file and save it. To run all those commands present in that file, we need to just write “bash file_name” in which commands are present. For example,
In this example, we can see that on the left side we created a file named script in which we have given some of the commands. In the terminal, when we run “bash script” we can observe that all those commands which were written in the file are executed. Please note that in this example I have given file name as “script” but you can give it whatever you want.
Now if we want the date to be printed every second we need to run it under a loop. The syntax of loop in scripting is very simple. for example,
In the above example, you can see how simple it is to use while loop in bash scripting. You just need to add do before the command and as the command is over done keyword is written. As we have not given any limits, so it will run infinitely until all your resources are exhausted. You can see in the below image, after running the script it is continuously printing the statement which is written in f1.txt file. To stop it we use CTRL + C.
Now let’s see what happens if we use this concept to build our new command for time. If we put our command which we created in while loop, let’s see what happens,
Here we can observe that we can see the time at runtime with seconds changing every second, but the major issue is that we are not able to use our terminal for other tasks. It is just showing the time only and if we want to run other commands we need to stop this script.
To solve this issue, we have to put the command in the background so that the script will run in the background and we can use the terminal for other commands. To put any task at the background we use “&” at the end of the commands. For example,
In the above picture when we run “gedit abhi” you can observe that it is using our terminal and we cannot run any other command in our terminal until we terminate it. As we use & after the command you can observe in the below picture that gedit is running in the background and our terminal is free. We are free to use our terminal for other commands also and at the same time gedit is also running.
Now, this was the concept of “&” for running anything in the background. Now we need to implement it to our new command so that we can make it quite more efficient. We are aware of using loops in scripting as I have discussed above. Now we just need to append “&” after “done” keyword to make it run in the background. Now if we add it, let’s see how it works,
Now you can see, it is working perfectly and we can use our terminal as well. In the below picture you can see that our live-time is also running and we can run other commands as well. Here I have used “bash livedate” command but you can save your file with any name like live-time, or anything you wish to. I am using livedate because I have used date command to build this live real-time command.