10 Tips for Writing Efficient Bash Scripts in Linux

(Click the public account above to quickly follow)

Author: Aaron Kili, Translation: Linux China/ch-cn

linux.cn/article-8618-1.html

If you have good articles to submit, please click → here for details

Shell scripting is the simplest way to learn or practice programming in Linux. It is an essential skill for system administrators who need to handle automation tasks and develop new simple utilities or tools (just to name a few).

In this article, we will share 10 practical tips for writing efficient and reliable bash scripts, which include:

1. Write More Comments in Your Scripts

This is a recommended practice that applies not only to shell scripts but also to all types of programming. Adding comments in your scripts can help you or others understand what different parts of the script do when reviewing it.

For beginners, comments are defined using the # symbol.

# TecMint is the best site for browsing various Linux articles

2. Exit the Script When a Command Fails

Sometimes, even if certain commands fail, bash may continue executing the script, which can affect the rest of the script (ultimately leading to logical errors). Use the following line to exit script execution when a command fails:

# Exit the script if a command fails

setoerrexit

# Or

sete

3. Exit the Script When Bash Uses an Unset Variable

Bash may also use unset variables, which can lead to logical errors. Therefore, use the following line to notify bash to exit the script execution when it tries to use an unset variable:

# Exit the script if an unset variable is used

setonounset

# Or

setu

4. Use Double Quotes to Reference Variables

Using double quotes when referencing (using the value of a variable) helps prevent word splitting due to spaces and unnecessary matches caused by recognizing and expanding wildcards.

See the example below:

#!/bin/bash

# Exit the script if a command fails

setoerrexit

# Exit the script if an unset variable is used

setonounset

echo“Names without double quotes”

echo

names=“Tecmint FOSSMint Linusay”

forname in$names;do

echo“$name”

done

echo

echo“Names with double quotes”

echo

forname in“$names”;do

echo“$name”

done

exit0

Save the file and exit, then run it as follows:

$ ./names.sh

10 Tips for Writing Efficient Bash Scripts in Linux

Using double quotes in the script

5. Use Functions in Your Scripts

Unless you are writing very small scripts (only a few lines of code), always remember to use functions to modularize your code and make the script more readable and reusable.

The syntax for writing functions is as follows:

functioncheck_root(){

command1;

command2;

}

# Or

check_root(){

command1;

command2;

}

When writing single-line functions, use a terminator after each command:

check_root(){ command1; command2; }

6. Use = Instead of == for String Comparisons

Note that == is synonymous with =, so use a single = for string comparisons, for example:

value1=tecmint.com

value2=fossmint.com

if[“$value1” = “$value2”]

7. Use $(command) Instead of the Old command for Substitution

Command substitution is replacing the command itself with the output of that command. Use $(command) instead of backticks command for command substitution.

This practice is also recommended by the shellcheck tool (which provides warnings and suggestions for shell scripts). For example:

user=`echo$UID`

user=$(echo$UID)

8. Use readonly to Declare Static Variables

Static variables do not change; their values cannot be modified once defined in the script:

readonly passwd_file=/etc/passwd

readonly group_file=/etc/group

9. Name Environment Variables in Uppercase and Custom Variables in Lowercase

All bash environment variables are named in uppercase letters, so use lowercase letters for your custom variables to avoid variable name conflicts:

# Define custom variables in lowercase, and environment variables in uppercase

nikto_file=$HOME/Downloads/niktomaster/program/nikto.pl

perl$nikto_file”h $1

10. Always Debug Long Scripts

If you are writing bash scripts with thousands of lines of code, debugging can become a nightmare. To make it easier to fix some errors before script execution, debugging is essential. Master this skill by reading the following guides:

  1. How to Enable Shell Script Debugging Mode in Linux (https://linux.cn/article-8028-1.html)

  2. How to Perform Syntax Checking Debugging Mode in Shell Scripts (https://linux.cn/article-8045-1.html)

  3. How to Trace Debugging Command Execution in Shell Scripts (https://linux.cn/article-8120-1.html)

This concludes the article. Do you have any other better bash scripting experiences to share? If so, please share them in the comment box below.

Did you gain something from this article? Please share it with more people

Follow ‘Linux Enthusiasts’ to enhance your Linux skills10 Tips for Writing Efficient Bash Scripts in Linux

Leave a Comment