tldr
This document is a brief overview how to structure your bash scripts. To get a more detailed explanation (e.g. arrays, subprocesses, etc.), please read the Styleguide.
Shebang
The first line of your script should be the shebang. This is the line that tells the system what interpreter to use to run the script. Always use #!/usr/bin/env bash
instead of #!/bin/bash
or #!/usr/bin/bash
because it's more portable.
Strict mode
The strict mode is a set of options that make the script more robust and less error-prone. It's a good practice to always use it.
Environment variables
If you have any environment variables that your script depends on, define/show them at the top of the script.
Use :=
to provide a default value for the variable. If don't want that the variable can be set by the user, and is only used internally, declare them directly. If these are imutable, use readonly
to make them readonly.
::: note
If you want to use the LOG_LEVEL
in sub processes, you have to export it with export LOG_LEVEL
.
:::
Constants
If you have any constants, define them at the top of the script.
Sourcing
If you have any functions or other scripts that you want to use in your script, source them at the top of the script.
Functions
Define your functions after the constants and sourcing. This makes it easier to find the main part of the script.
Declare your function parameters with local
to make them local to the function. You may use -r
to make them readonly.
Use :-
to provide a default value for the parameter.
Namespacing
If you have a lot of functions, you can namespace them by using a prefix.
Variables and name conventions
Use lowercase for variable names. Use _
to separate words. Enclose the variable name in ${}
to make it clear where the variable name starts and ends. Additionally, this makes it easier to use Bash expansions.
External commands
Try to avoid external commands like grep
, awk
, sed
, etc. Use Bash builtins instead, fall back to coreutils if necessary.
If you are using external commands, make sure to check if they are available or provide them (e.g. with the binary
library).
Test expressions
Use [[
instead of [
for test expressions. It's more powerful and has less surprises. Use arithmetic expressions ((
for arithmetic operations/tests.
If you use assert like checks, make sure to use ||
instead of &&
to make the script not fail if the check fails.
[[ <expr to be true or ...> ]] || { echo "do something else"; }
Main
The main part of the script should be at the bottom of the script. This is where you call the functions and do the work.
Call main
Call the main function at the bottom of the script. Check if the script is being run directly or sourced.