#!/bin/bash function helpexit() { echo "Simple Timer." echo '$PREFIX is printed before the time' echo '$POSTFIX is printed after the time' exit } for (( i=1; i<=$#; i++ )); do [[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "-h" ]] && helpexit done function displaytime { local T=$1 local D=$((T/60/60/24)) local H=$((T/60/60%24)) local M=$((T/60%60)) local S=$((T%60)) printf '\r%s[' $PREFIX (( $D > 0 )) && printf '%dd ' $D (( $T > 3599 )) && printf '%02d:' $H printf '%02d:%02d]%s\r' $M $S $POSTFIX } while true; do displaytime "$SECONDS" >&2 sleep 1 # die if parent dies kill -0 $PPID 2>/dev/null || exit done