Templates I use in my scripts.

Reacting to Ctrl+c (SIGINT) with trap#

control_c() {
    echo  # new line for ^C character that is printed
    echo "Exiting."
    exit 1
trap control_c SIGINT

or doing something in any case:

set -e

finally() {
    echo finally
trap finally EXIT

echo before error
false  # change me to true and see what happens
echo after error

Looping Directories Containing Whitespace#

IFS=$'\n' dirs=$(ls /tmp)
for dir in $dirs; do
    echo $dir