X

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 for writing a bash script to enhance the efficiency of the bash script and make it more readable.

Commenting Bash script code

Commenting is the most essential part of clean code which determines or explains various aspects 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 in Bash

A function is a set of commands bundled together to perform a 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 in Linux Shell scripts

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

Terminating a Bash Script On Error

Sometimes 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 in Bash

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}
}

The use of Curly Braces in Bash scripting

Bind the variables with curly braces while using the variables concatenation with string to avoid unnecessary variable uses. This also helps in the 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"

Bash Command Substitution

When assigning the command output 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 Conventions in Shell scripts

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 in Bash

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 read-only command.

#!/bin/bash

# This is nginx test host config

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

Comparing Strings in Nash Shell scripts

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 strings using single ‘=’.

#!/bin/bash

# comparing two strings

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

  echo "This is test purpose".

fi

Bash Script Debugging

Debugging is the most essential part of identifying the issue. We can check the script syntax error, so to check it, we need to run the bash script with -n using the 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 preferable to others.

FAQForge Staff:
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings