[‘
Shell scripting is the easiest form of programming you can learn/do in Linux. More so, it is a required skill for system administration for automating tasks, developing new simple utilities/tools just to mention but a few.
n
In this article, we will share 10 useful and practical tips for writing effective and reliable bash scripts and they include:
n
1. Always Use Comments in Scripts
n
This is a recommended practice which is not only applied to shell scripting but all other kinds of programming. Writing comments in a script helps you or some else going through your script understand what the different parts of the script do.
n
For starters, comments are defined using the #
sign.
n
#TecMint is the best site for all kind of Linux articlesrn
n
2. Make a Script exit When Fails
n
Sometimes bash may continue to execute a script even when a certain command fails, thus affecting the rest of the script (may eventually result in logical errors). Use the line below to exit a script when a command fails:
n
#let script exit if a command failsrnset -o errexit rnORrnset -ern
n
3. Make a Script exit When Bash Uses Undeclared Variable
n
Bash may also try to use an undeclared script which could cause a logical error. Therefore use the following line to instruct bash to exit a script when it attempts to use an undeclared variable:
n
#let script exit if an unsed variable is usedrnset -o nounsetrnORrnset -urn
n
4. Use Double Quotes to Reference Variables
n
Using double quotes while referencing (using a value of a variable) helps to prevent word splitting (regarding whitespace) and unnecessary globbing (recognizing and expanding wildcards).
n
Check out the example below:
n
#!/bin/bashrn#let script exit if a command failsrnset -o errexit rnrn#let script exit if an unsed variable is usedrnset -o nounsetrnrnecho "Names without double quotes" rnechornnames="Tecmint FOSSMint Linusay"rnfor name in $names; dorn echo "$name"rndonernechornrnecho "Names with double quotes" rnechornfor name in "$names"; dorn echo "$name"rndonernrnexit 0rn
n
Save the file and exit, then run it as follows:
n
$ ./names.shrn
n

n
5. Use functions in Scripts
n
Except for very small scripts (with a few lines of code), always remember to use functions to modularize your code and make scripts more readable and reusable.
n
The syntax for writing functions is as follows:
n
function check_root(){rntcommand1; rntcommand2;rn}rnrnORrncheck_root(){rntcommand1; rntcommand2;rn}rn
n
For single line code, use termination characters after each command like this:
n
check_root(){ command1; command2; }rn
n
6. Use = instead of == for String Comparisons
n
Note that ==
is a synonym for =
, therefore only use a single =
for string comparisons, for instance:
n
value1=”tecmint.com”rnvalue2=”fossmint.com”rnif [ "$value1" = "$value2" ]rn
n
7. Use $(command) instead of legacy ‘command’ for Substitution
n
Command substitution replaces a command with its output. Use $(command)
instead of backquotes `command`
for command substitution.
n
This is recommended even by shellcheck tool (shows warnings and suggestions for shell scripts). For example:
n
user=`echo “$UID”`rnuser=$(echo “$UID”)rn
n
8. Use Read-only to Declare Static Variables
n
A static variable doesn’t change; its value can not be altered once it’s defined in a script:
n
readonly passwd_file=”/etc/passwd”rnreadonly group_file=”/etc/group”rn
n
9. Use Uppercase Names for ENVIRONMENT Variables and Lowercase for Custom Variables
n
All bash environment variables are named with uppercase letters, therefore use lowercase letters to name your custom variables to avoid variable name conflicts:
n
#define custom variables using lowercase and use uppercase for env variablesrnnikto_file=”$HOME/Downloads/nikto-master/program/nikto.pl”rnperl “$nikto_file” -h “$1”rn
n
10. Always Perform Debugging for Long Scripts
n
If you are writing bash scripts with thousands of lines of code, finding errors may become a nightmare. To easily fix things before executing a script, perform some debugging. Master this tip by reading through the guides provided below:
n
- n
- How To Enable Shell Script Debugging Mode in Linux
- How to Perform Syntax Checking Debugging Mode in Shell Scripts
- How to Trace Execution of Commands in Shell Script with Shell Tracing
n
n
n
n
That’s all! Do you have any other best bash scripting practices to share? If yes, then use the comment form below to do that.
n
‘]