Bash

In bash, when assigning variables, do not use a space after the equals.

Conditional statements

For the condition, [] are POSIX compliant, double [[]] are supported by shells and allow for additionl operators such as ||. Use [] for the best portablity, but otherwise [[]].

if (condition);
then
echo "this when condition is met"
else
echo "this when condition is not met"
fi

Echo an argument

echo "Echo the first argument $1"

Raise an error

Raising an error is done by returning a non-zero exit code.

exit 1

Check if variable is empty

if [[ -z "$var" ]]; then
    echo "Empty"
else
    echo "$var"
fi

Modulation

The ! is a NOT operator.

if ! (($1 % 7)); then
    echo "$1 is divisible by 7"
else
    echo "$1 is not divisible by 7"
fi

Check length of string

This can be done either by the number of characters or by bytes. source

echo -n test | wc --bytes
4
echo -n test | wc --chars
4