Giter Club home page Giter Club logo

bash-scripting-the-definitive-guide's Introduction

Bash-Scripting-The-Definitive-Guide

Streamline your Bash scripts with expert guidance on best practices and code organization.


Bash Scripting Best Practices

1. Shebang Line

Start your script with a shebang line to specify the interpreter:

#!/bin/bash

2. Script Info

Include script information such as name, date, author, and description:

################################################################################
# Name:        myscript.sh
# Date:        2024-04-14
# Author:      Sam
# Description: My awesome script.
################################################################################

3. Figlet or Toilet

Use figlet or toilet to generate the name of the script in large letters, commented out:

figlet "My Script" | sed 's/^/# /'
# ▙▗▌▌ ▌  ▞▀▖▞▀▖▛▀▖▜▘▛▀▖▀▛▘
# ▌▘▌▝▞   ▚▄ ▌  ▙▄▘▐ ▙▄▘ ▌ 
# ▌ ▌ ▌   ▖ ▌▌ ▖▌▚ ▐ ▌   ▌ 
# ▘ ▘ ▘▀▀▀▝▀ ▝▀ ▘ ▘▀▘▘   ▘ 

4. Source External Scripts

In this section of your script, you should source external scripts that provide additional functionality. Use the full path to the script and include a comment explaining what it does, along with a link to the project page for reference.

Example:

# Source external scripts for additional functionality

# Get extract flags from arguments as variables using: https://github.com/AmosNimos/flag
. /path/to/flag_utility
flag_utility "$@"

Make sure to replace /path/to/flag/utility with the actual path to the external script in your system. This practice helps maintain transparency and allows users to understand the purpose of the sourced script.

5. Print Help

Add a function to print the script's help information:

print_help() {
    echo "Usage: $0 [OPTIONS]"
    echo "Options:"
    echo "  -h, --help    Show help information"
}

You can use a here document (heredoc) to create multi-line strings in Bash, which is particularly useful for printing help messages. Here's an example of how you can define the help message using a heredoc:

print_help() {
    cat << EOF
Usage: $0 [OPTIONS]

Options:
  -h, --help    Show help information
  -v, --version Show version information
EOF
}

In this example, cat << EOF starts the heredoc, and EOF marks the end of the heredoc. The text between EOF and EOF will be treated as a multi-line string and printed by cat. You can adjust the help message content as needed.

6. Initializing Global Variables

Initialize global variables using capital letters and no spaces:

GLOBAL_VARIABLE="value"

7. Reading Arguments

Read and parse command-line arguments using if or case statements:

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    print_help
    exit 0
fi

8. Functions

Define your functions after argument parsing:

my_function() {
    local local_variable="value"
    echo "Function executed"
}

10. Additional Notes

  • Use comments generously to explain your code's logic and purpose.

bash-scripting-the-definitive-guide's People

Contributors

amosnimos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.