How to create scripts in Linux

For many users, getting into everything related to open source operating systems , that is, Linux distributions, is quite a challenge. However, once we get used to its use and gain some experience, the truth is that we will be able to obtain great results and a pleasant experience of use, as many of you have already verified. Now we are going to see how to create our own scripts in Linux and thus improve its use.

Things have changed a lot compared to the situation with Linux distributions that we found a few years ago. While in the past the use of these systems was limited to a few professionals, today it is affordable software for the majority. This is largely achieved thanks to the work of the developers of these distros who try to make it easier for us to access and use their projects. Obviously the creators of these distributions want to bring their projects closer to as many users as possible. At the same time, to a great extent, they want to eliminate those certain fears that those who do not know these operating systems initially have.

How to create scripts in Linux

At first, the first thing we do is choose the distribution that best suits our needs for download and installation on the computer. There are many to choose from, and keep in mind that this is an important decision. This is something that is usually much clearer to Linux regulars and professionals in this type of system, but not so much to novice users. We can say that right now we have the ability to make basic use of Linux, but if we have the relevant knowledge, we can also use it in a more advanced way.

For users who want to delve into this open source software as much as possible, below, we will talk about a utility that can help you a lot. This is something that is especially evident among all those who want to get the most out of their open source system .

What do I need a script for in Linux

Specifically, we are referring to the possibility of creating our own scripts in Linux in the simplest and most affordable way possible. Once we have the basic concepts for this type of project clear, we can expand them and delve into other more advanced and professional scripts, or improve those that we created at the beginning. To give you a better idea of what we are talking about, it is worth mentioning that a script is actually a text file that contains certain commands or keywords of the operating system Shell . All this is often used many times to automate certain tasks and create more complex ones.

In this way, what we achieve is to automate certain tasks to be carried out in the open source system and thus save efforts and gain time . In short, what we are going to create is our own file through a series of commands that we are adding and that will be essential for the subsequent execution of the set. Once we have finished it, we find a small program that will be of great help to us when it comes to automating certain tasks or actions on the Linux-based computer.

Therefore, if we ask ourselves the reason for creating a script in Linux , the answers can be practically endless. And it is that depending on the commands that we add to them, we can carry out all kinds of tasks in the operating system. Obviously here we can automate both basic and more advanced functions, everything will depend on our knowledge on the subject. Some more advanced users with deep knowledge of the system sometimes create some really spectacular scripts.

What do I need to create a file and automate tasks

As happens in any other operating system in which we want to create our own script, the first thing we are going to need here in Linux is going to be a program that acts as a text editor . Keep in mind that most distributions make an application with these characteristics available to us for all kinds of tasks related to these contents. We will also have the possibility of adding some other program of these office automation characteristics that we find more comfortable.

Generally, the text editor included in the most used Linux distributions is found in the utilities section. One of the most widespread examples in this sense can be found with the program called Gedit .

gedit Linux

This, as you can imagine, is a key element that we are going to need when creating our own script in Linux. This is where we are going to enter our orders or commands for later execution.

Components to include in Linux scripts

As we told you, precisely in this program in which we are going to type the commands and orders that are going to be part of the automation file. Obviously for all this that we tell you it is essential that we have some knowledge about the commands that we are going to use to create this small program in the open source system.

Of course, so that later the operating system can interpret that we intend to execute a script in Linux , let’s see how to start. The first thing we must add to the new file that we create in the text editor is the following:


With this order at the beginning of the file, we indicate to the operating system a series of commands and orders belonging to a script. Next, our experience and knowledge of Linux will come into play when developing this element. And it is that after that start we introduce the corresponding commands that we are going to execute in batches, each one in a different line.

Also, to document the new file we are creating, we can use the Echo command. So we are going to indicate the texts that will appear in the console and that will help us in the future.

Type of file to create the scripts

There is another section that we must keep in mind so that the new script that we have created in Linux does not give us problems. We talked about the file format that we are going to create for it. As is usual in other types of files, here we must use a specific format so that the operating system can interpret it.

Specifically for this type of software component we will have to save the new file that we have created in .SH format. Obviously, we store all this in the disk path in which we want to save the different scripts that we create here.

It is worth mentioning that this file format is similar to the .bat that we create in Windows to execute batch files. Therefore, once we have saved the file in question , we will have the project we were looking for ready. In the future we will only have to reopen those scripts that we have created with a text editor such as the one mentioned. So we can edit or modify them as much as we want.

Our first script

Surely those of you who at the time began to program or are beginning at this moment to introduce yourselves to this world, know the popular phrase Hello world . Well, to give you an idea we are going to create a script in Linux as we have mentioned in these lines with this same text. To do this, the first thing we will do is open the corresponding plain text editor that we have in the Open Source distribution.

Once we have the work desktop on the screen , we are going to type the corresponding content in our script, line by line:

#!/bin/bash # Este es nuestro primer script de Linux echo Hola Mundo

Once we have saved this new element in the format mentioned above, IA will be able to execute it from the Linux terminal.