linux Tutoriaux data-src-img=

The shell script is the most simple type of programming you would write on Linux. Moreover, it is a necessary skill to automate your system administration tasks, to develop new simple features, …
In this tutorial, we are going to introduce 10 tips to write efficient and reliable scripts.

Comment your scripts

More an advice than a tip, commenting your code is valid for any type of programming.
Writing comments of your scripts will help you (or will help the one editing your code) to better understand the different sections composing it.
For beginners, comments in shell script start with the # icon :

#This is a comment

Please plan a script exit in case of failure

It can happen that a command of your scripts fails but your bash might continue to execute it, possibly affecting the rest of the actions/events of your script, up to the crash. Use these lines to leave your script if a command fails :

#Script exit in case of a command failure
set -o errexit
set -e

Stop a script if the bash uses an variable not defined

The bash can also try to use an unregistered variable and result into a program crash.
To avoid it, use the following lines to indicate to your script to stop if it tries to use an unregistered variable :

#Script exit in case of an unregistered variable use
set -o nounset
set -u

Use the double inverted commas to refer to a variable

Using the double inverted commas when you use a variable value will prevent you from a term separation (because of the spaces) or detecting special characters and excluding them from your variable :

echo "Terms without the double inverted commas"
names="Jaguar Network Community"
for name in $names; do
echo "$name"
echo "Terms with the double inverted commas"
for name in "$names"; do
echo "$name"
exit 0

Safeguard ( and execute your scrip :

root@jn-community:~# ./
Terms without the double inverted commas


Terms with the double inverted commas

Jaguar Network Community

Use the features in your scripts

It is better to use functions to partition your code and make it clearer as soon as it gets more complex :

#1st function
function my_function(){

#2nd function

#3rd function
my_function(){ command1; command2; }

Use = instead of == for the comparisons of strings

Please notice that == is equivalent to = and hence, you can use a simple = for the comparisons of strings :

if [ "$value1" = "$value2" ]

Use $(command) instead of ‘command’ for substitutions

The command substitution replaces a command steered by the result. Use $(command) instead of the simple quotes command to substitute the command :

user=`echo “$UID”`
user=$(echo “$UID”)

Use “Read-Only” to report static variables

A satis variable does not change, its value can not be modified once defined :

readonly passwd_file=”/etc/passwd”
readonly group_file=”/etc/group”

Use upper case letters for the variables of environment and lower case letters for the personalized variables

All the variables of environment of the bash are informed in upper case letters, therefore, use the lower case letters created for your needs to avoid any conflicts :

perl “$my_script” -h  “$1”

Always perform a debugging for the long scripts

If you write bash scripts with a consequently number of code lines, finding errors will be complicated. To correct your errors before executing a script, perform a debugging :

bash -n line 9: syntax error: unexpected end of file