12 Best Practices for Writing Bash Scripts

Bash shell refers to Bourne Again Shell which can be found as the default shell in most of the Linux distributions. A Bash Script is a file where multiple shell commands are scripted to perform a particular task. If you are familiar with bash script then this article is for you, in this demonstration I have included 12 best practices to write a bash script to enhance the efficiency of the bash script and make it more readable.

Commenting

Commenting is the most basic part of clean code which determines or explains various parts of complex codes. While writing the script it is effortless to get confused over the code you have written as time passes. It’s also effective while working in a group in a big project as it helps to understand what the function or method actually does.

Working With Functions

A function is a set of commands bundled together to perform the specific task which helps in modularizing the workflow and making the script reusable eliminating the code repetition. It makes code clean and more readable as well as maintainable

#!/bin/bash

function check_root() {

  echo "function has been called";

}

Reference Variables With Double Quotes

When using double quotes will help to eliminate unnecessary globbing as well as word splitting including whitespace when variable values contain separator character or whitespace. In the following example, we can distinguish the difference in using variables with and without quotes.

Terminating Script On Error

Some time while executing the script there might be some execution failure with the command. Even if a command fails to run the script may continue to run affecting the other commands in the script. So to prevent any further logical error we need to include set -o errexit or set -e to terminate the command on error.

#!/bin/bash

# Terminate the script on command fails

set -o errexit

Terminating Script On Undeclared Variable Uses

During script execution, if there is an undeclared variable then bash may use the variable which leads to logical error. In Order to terminate the script when bash invokes undeclared variables use the following line to instruct the bash to do so.

#!/bin/bash

# Terminate the script on command fails

set -o nounset

Declaring Variables

We must declare the variable according to its data type and uses. When variables remain undeclared the bash might fail to execute the command related to it. Variables can be declared either globally or locally in the script.

#!/bin/bash

# variable declaration

declare -r -i x=30

function my_variable(){

  local -r name = ${HOME}

}

Use Curly Braces

Bind the variables with curly braces while using the variables concatenation with string to avoid the unnecessary variable uses. This also helps in easy identification of the variable while using it in string. For example:

#!/bin/bash

# Terminate the script on command fails

set -o errexit

# Custom variable

data= "${USER}_data is being used"

Command Substitution

When assigning the output of the command to the variable bash uses the command substitution feature. We need to use $() instead of backquotes to assign the output to variables as is recommended.

#!/bin/bash

# Terminate the script on command fails

set -o errexit

#Displaying Date

date_now = ${date}

Variables Naming Convention

In our system, all the environment variables are named using uppercase letters. So when we declare a local variable we need to declare it using lowercase letters to avoid conflict between environment and local variable name.

#!/bin/bash

# Terminate the script on command fails

set -o errexit

user_var = "$HOME is your system's current login user."

Declare Static Variables

If you have static data that remains unchanged over the entire script, you can assign the value to a static variable whose value cannot be altered. You can declare the static variable using the readonly command.

#!/bin/bash

# This is nginx test host config

readonly test_conf_path = "/etc/nginx/conf.d/test.conf"

Comparing Strings

In most of the scenarios string is compared using double ‘==’ but in bash script string comparison work is done by single ‘=’. In the following example, I have compared two string using single ‘=’.

#!/bin/bash

# comparing two strings

If [ "$HOME" = "User1" ]; then

  echo "This is test purpose".

fi

Script Debugging

Debugging is the most essential part to identify the issue. We can check the script syntax error so to check it we need to run the bash script with -n using bash command.

$ bash -n script_name

Also, we can enable and run the script in debug mode using the following command.

$ bash -x script_name

Conclusion

Here, these are the bash script 12 scripting practices you should adopt to improve your scripting. When working with commands to perform specific tasks bash scripting is more preferable to others.

Leave a Comment

*