contador web Skip to content

5 PRO Tips for Shell Script

Diolinux

They say that after you learn Terminal, you really start to understand Linux deeply. Whether this is true or not, in fact, it is true that mastering the terminal gives you some advantages over what you can do with your system. Today you will check 5 days for those who want to be a PRO in Shell Script.

1 – Portability of your Shell Script

We often automatically indicate Shebang’s path as #! / Bin / bash, however, if Bash is installed in a different directory (such as on systems like FreeBSD, OpenBSD and some Linux distros) she will probably not be able to find the interpreter and its program / script will not work.

Therefore, the best way for you to avoid this type of problem is to indicate the env path that is standard on all systems and it is responsible for redirecting to the path where the interpreter is located. So, always try to start your programs / scripts with the following path after Shebang:

#!/usr/bin/env bash

The same goes for other Shells: ZSH, FISH, etc.

2 – Always try to put header in your scripts

Headings are important so that the person knows what his / her program is for, how to contact him / her in case of any problem, how to use it and among several other positive points.

Generally, in a header there should be “help”, “changelog”, “TODO”, “FIXME”,… that is, several necessary information. However, in summary, some information is essential, such as: Description of the script; Author; Version and License. Below is a minimal and basic example of a header:

# autor: Nome Sobrenome 
# descrição: O que seu Script/Programa faz 
# version: 1.0
# licença: MIT License

The example above is in Portuguese, but it is interesting to leave it in English, to become even more global.

3 – Separate by function

The initial reason for Object Oriented Language is organization. However, procedural or structural languages ​​can maintain the organization by separating each «transformation» into functions. In Shell Script, you can declare a function in two forms of syntax, see both forms below:

USING the word function

function my_function () {

echo “My role”

}

OR WITHOUT USING the word function

my function(){

echo “My role”

}

To call the function, just call the function name in the script, eg my_function

4 – Leave your code with indentation

In Shell Script, as in most programming languages, indentation is not mandatory, however, it is interesting to keep your code more organized, so always try to keep your code indented.

A nice tip is if you use the Vim editor, just select everything by pressing the key ESC and then combining the keys ggVG , after everything is selected, just double-tap the equal sign == and your code will be automatically indented. Magic! ?

5 – State descriptive names

When creating names of variables and functions, try to describe (not so much, logical) and separating with underline ** _ **, as it will be more professional and less likely to have conflict of names and unpleasant situations, some examples of how to describe: set_name, get_name, display_info_start, etc.

If you haven’t mastered Shell Script and the Linux Terminal. Take advantage of a promotion where you can purchase 5 LINUX TERMINAL COURSE including Shell Script, Regular Expressions, Vim and Sed. Clicking on this link http://bit.ly/Promo5pg to purchase with PagSeguro or on this link http://bit.ly/5CursosLinux to purchase with PayPal. If you want more details of the promotion see this post: 5 Linux Terminal Courses for you! .

This article was written in partnership with the guys from Terminal Root, who are the authors of the courses mentioned above, until next time!


Have you seen any errors or would you like to add any suggestions to this article? Collaborate, click here.