My Coding Rules for Bash Scripts

Published: (January 19, 2026 at 05:39 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for My Coding Rules for Bash Scripts

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:

SymbolDescription
$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

OperatorMeaning
==Equal
!=Not equal
>Greater than (lexicographic)
if [ "$string1" == "$string2" ]; then
    echo "The strings are identical"
fi

Numeric Comparison

OperatorMeaning
-eqEqual
-neNot equal
-ltLess than
-gtGreater than
-leLess than or equal
-geGreater 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.
Back to Blog

Related posts

Read more »

Linux Tutorial: Logs to CSV to JSON

Setup Directory bash mkdir -p tutorial cd tutorial Generate Sample Logs bash Generate logs inside the tutorial folder echo '2026-01-18 05:42:09 | INFO | system...

A Linux Tutorial: Log to CSV to JSON

Overview This tutorial walks through the process of converting raw application logs into structured JSON data. The workflow is useful for generating test data...

React Coding Challenge : TypeHead

Zeeshan Ali !ZeeshanAli-0704https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws...