How I write bash

This is a short guide to help me write bash scripts since I usually tend to review my previous code in order to remember how I write Bash.

Template file

This is what I usually start every bash script with:

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

The shebang subject has been discussed about quite a bit.

The others are flags that make errors easier to debug.

Conditions

Arithmetic

if (( variable <= 0 )); then
    echo "HI"
fi

Strings

Check if a string exists

if [[ -n ${variable} ]]; then
    echo "HI"
fi

Loops

for file in $(ls); do
    echo $file
done
STRINGS='one two'

for STRING in $STRINGS; do
    echo $STRING
done