My Coding Rules for Bash Scripts
Source: Dev.to

This collection now includes examples of simple and complex functions, with and without parameters, providing a comprehensive reference for coding Bash scripts. It starts from the basics and progresses to less‑used solutions.
1. Shebang
Start each script with a shebang line to indicate the interpreter to use:
#!/bin/bash
2. Permissions
Ensure that the script has execution permissions:
chmod +x script_name.sh
3. Comments
Use comments to explain the code:
# This is a comment
4. Variables
Variable Declaration
Declare variables without spaces around the equal sign:
name="value"
Default Values – Use ${variable:-default_value} to set a default if the variable is not defined:
name=${name:-"default_value"}
Variable Processing
Uppercase
uppercase=${name^^}
Lowercase
lowercase=${name,,}
Capitalization (first letter uppercase)
capitalize=${name^}
Inline Character Replacement – Replace all occurrences of a substring:
new_name=${name//old/new_value}
5. Input Parameters
Bash scripts can receive input parameters, which can be processed in various ways:
| Symbol | Description |
|---|---|
$1, $2, … | Positional parameters ($1 is the first argument, $2 the second, etc.) |
$# | Number of parameters passed |
$@ | All parameters as a list (useful in loops) |
$* | All parameters as a single string |
$? | Return code of the last executed command |
Examples
# $1, $2, …
echo "First parameter: $1"
# $#
echo "Number of parameters: $#"
# $@
for param in "$@"; do
echo "Parameter: $param"
done
# $*
echo "All parameters: $*"
# $?
echo "Return code: $?"
6. Conditions
Use if … then … else … fi for conditional structures:
if [ condition ]; then
# commands
else
# other commands
fi
7. case (Switch‑Case) Structures
Use case to handle multiple conditions more readably. Separate possible values with spaces:
case $variable in
value1 | value2 | value3)
# commands for value1, value2, or value3
;;
value4)
# commands for value4
;;
*)
# default commands
;;
esac
8. Functions
Simple Function (no parameters)
my_simple_function() {
echo "This is a simple function."
}
# Call the function
my_simple_function
Function with Parameters
my_function_with_parameters() {
param1=$1
param2=$2
echo "Parameter 1: $param1"
echo "Parameter 2: $param2"
}
# Call the function with parameters
my_function_with_parameters "value1" "value2"
Complex Function (processing & multiple parameters)
my_complex_function() {
name=$1
age=$2
if [ "$age" -lt 18 ]; then
echo "$name is a minor."
else
echo "$name is an adult."
fi
}
# Call the complex function
my_complex_function "Alice" 20
my_complex_function "Bob" 15
9. Variable Testing
Comparators for Tests
String Comparison
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
> | Greater than (lexicographic) |
if [ "$string1" == "$string2" ]; then
echo "The strings are identical"
fi
Numeric Comparison
| Operator | Meaning |
|---|---|
-eq | Equal |
-ne | Not equal |
-lt | Less than |
-gt | Greater than |
-le | Less than or equal |
-ge | Greater than or equal |
if [ "$number1" -lt "$number2" ]; then
echo "$number1 is less than $number2"
fi
10. Loops
Use for, while, or until loops to iterate:
for i in {1..5}; do
echo "$i"
done
11. Error Handling
Check for errors with $? and use set -e to stop the script on any error:
set -e
12. Using set
Treat undefined variables as errors with set -u:
set -u
13. Output
Use echo to display messages or results:
echo "Message"
14. Using trap
Handle signals and perform cleanup before exiting:
trap 'cleanup_command' EXIT
15. Best Practices
- Avoid ambiguous variable names.
- Use descriptive file names.
- Test your scripts in a safe environment before running them in production.