Skip to content

Bash Coding Standard

Adam Szablya edited this page Jun 2, 2024 · 1 revision

Metrolla has adopted the Google Coding Standard for Bash scripts.

Google's style guide for Bash scripting emphasizes writing clean, maintainable, and portable shell scripts. Below is a summary of the key points from the guide:

Google Bash Style Guide

1. General Principles

  • Clarity: Write clear and understandable scripts.
  • Consistency: Follow established coding conventions and structures.
  • Portability: Ensure scripts are portable across different environments.

2. Shebang

  • Use #!/bin/bash for Bash scripts.
    #!/bin/bash

3. File Extension

  • Use .sh as the file extension for shell scripts.

4. Comments

  • Use comments to explain the purpose of the script, significant sections, and complex logic.
    # This script calculates the sum of two numbers.

5. Formatting

  • Indentation: Use 2 spaces for indentation. Avoid tabs.
    if [ "$condition" ]; then
      echo "Condition met"
    fi
  • Line Length: Limit lines to 80 characters.
  • Line Breaks: Use line breaks to separate logical sections of the script.

6. Variables

  • Naming: Use lower_snake_case for variable names.
  • Initialization: Always initialize variables.
    my_variable="value"
  • Readonly Variables: Use readonly for constants.
    readonly MY_CONSTANT="constant_value"

7. Quoting

  • Double Quotes: Use double quotes to prevent word splitting and globbing.
    my_var="some value"
    echo "$my_var"
  • Single Quotes: Use single quotes for literals that don't need variable interpolation.
    echo 'Literal string'

8. Tests

  • Use [ (also known as test) for testing conditions, with spaces around the brackets.
    if [ "$var" -eq 1 ]; then
      echo "Variable equals 1"
    fi

9. Functions

  • Define functions using function keyword.
  • Use lower_snake_case for function names.
  • Place all function definitions at the start of the script.
    function my_function {
      echo "Function body"
    }

10. Error Handling

  • Use set -e to exit on error.
  • Use set -u to treat unset variables as an error.
  • Use trap to clean up temporary files and handle signals.
    set -e
    set -u
    
    trap 'rm -f /tmp/my_temp_file' EXIT
    
    function cleanup {
      rm -f /tmp/my_temp_file
    }
    
    trap cleanup EXIT

11. Input and Output

  • Use read for input.
  • Use echo for output.
    read -p "Enter your name: " name
    echo "Hello, $name"

12. Scripts Structure

  • Structure scripts with the following sections:
    1. Shebang
    2. Comments and License Information
    3. Imports and Global Variables
    4. Functions
    5. Main Script Execution

13. Portability

  • Avoid using Bash-specific features if possible.
  • Prefer POSIX-compliant code for better portability.

Example Script

Here is an example that illustrates some of these standards:

#!/bin/bash

# This script demonstrates a simple greeting.

# Constants
readonly SCRIPT_NAME=$(basename "$0")

# Function to print usage
function print_usage {
  echo "Usage: $SCRIPT_NAME <name>"
}

# Main function
function main {
  if [ "$#" -ne 1 ]; then
    print_usage
    exit 1
  fi

  local name="$1"
  echo "Hello, $name"
}

# Script entry point
main "$@"

For the most detailed and up-to-date information, refer to the full Google Shell Style Guide.

Clone this wiki locally