fbpx What Is Shell Script? How to Create and Run on Linux | Certificação Linux

What Is Shell Script? Learn and See Real-World Examples

📅 11/11/2021 ⏱ 5 min ✍️ Uira Ribeiro
Linux
What Is Shell Script? Learn and See Real-World Examples
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

What is shell script? This can be a fairly common question for those starting out with the Linux system.

Linux offers various solutions for the Information Technology field, making the lives of system administrators much easier.

However, not all IT professionals are aware of all its advantages and the tools that can be incorporated into it.

Among these is Shell Script, which is very useful in the task automation process. And in this article we will explain what it is and practical examples of application.

So, what is a shell script file?

We call shell script the files that will be interpreted by Shell-type programs.

With it, it's possible to create various commands for Linux. In other words, create tasks that will be performed from time to time, automating this process, in addition to initialization scripts and other functionalities.

Scripts

Whenever an actor is going to perform a scene, whether in movies, television, or theater, they receive a script. A text that contains various information about how they should act during the scene, the important points for the unfolding of the narrative.

Scripts on a computer work in a very similar way. They are a series of instructions that dictate how the device should execute tasks x, always according to what was programmed.

The lines of code, within the programming language, can use a script to, for example, count the number of daily accesses, whether it's done from desktop or mobile, geolocation, and much more.

Scripts can, of course, go far beyond that. With them, it's possible to change the look of the page, colors, layout, and even, as mentioned above, add useful functions to the page.

Shell

In general, we can say that Shell is a layer that enables users to access and configure commands in the operating system.

To better understand, imagine a turtle. It has a shell that surrounds and protects it from the outside world. That way, its actual body doesn't suffer major damage.

In practical terms, Shell works as a kind of intermediate interface between the user and the operating system. It has selective functions and only allows the execution of actions according to each user's access level.

However, basically, there are two types of Shell: CLI and GUI.

CLI

Abbreviation for "Command Line Interface", that is, those that have a command line-based interface. In other words, in order for any action to be executed, an equivalent command must be typed.

GUI

Abbreviation for "Graphical User Interface". In this case, the graphical interface is used so that the same commands don't need to be typed more than once.

In summary, whether through a graphical interface or with command lines, Shell performs basic operating system actions in order to better manage computer data.

This can happen with files stored on the device or even in the execution of other applications.

Shell script language structure: main aspects

shell script language structure

Now that we know what shell script is, let's dive deeper into its language!

Just like other programming languages, the shell script language has variables in its structure, in addition to decisions and loops. Check out some of them!

Variables

Basically, almost all variables in the Shell Script language are string type. It's possible, of course, to convert the variable type depending on the type of operation you want to perform.

There is the possibility to create arrays, perform arithmetic operations, among other functions.

However, in the case of variables that are actually string type, it's recommended that you use quotes so that there is no risk of the interpreter trying to execute the string as a form of command.

Operators

Performing mathematical calculations in Shell Script can happen through various arithmetic operators:

  • + Addition
  • – Subtraction
  • * Multiplication
  • / Division
  • % Modulo
  • ** Exponentiation

However, performing the operations is not such a simple task. Variables follow the string standard, which means some maneuvers are necessary to execute the operations.

See below how to perform arithmetic operations.

  • variavel=2
  • variavel2=3
  • variavel3="CertificacaoLinux"
  • tentativaSoma=$variavel+$variavel2
  • echo "Valor da variavel3 é: $variavel3"
  • echo "Valor da tentativaSoma é: $tentativaSoma"

When we talk about comparison operators, Shell Script has somewhat different characteristics from other programming languages. See below:

  • -eq (equal): Equal to;
  • -ne (not equal): "Not equal" or Different from;
  • -lt (less than): Less than;
  • -gt (greater than): Greater than;
  • -le (less or equal): Less than or equal to;

Decision structures

Every programming language makes use of decision structures to control the flow of the system through logical decisions.

These decisions are written through logical expressions that use comparison operators to evaluate whether that expression is true or not.

Applications of shell script programming

shell script application

Shell script can implement various applications very useful to the system. We listed some of them below!

Task automation

With shell script, it's possible to automate your machine's routine tasks, such as system updates, performing upgrades, cleaning, etc.

File manipulation

It's also possible to create, edit, find, and organize files through shell script.

Data backup

Shell script can also be used to configure directories so that automatic backups are made, and insert utility arguments into them.

That way, inactive files can be moved or copied to another location.

Conclusion

Is it clear what shell script is? It is an excellent tool that "makes" the user's and administrator's life easier. After all, task automation speeds up deliveries and increases reliability in the process.

And to learn how to apply shell script in Linux, come with us and learn Linux with the most complete Teaching Platform on the market!

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp