#!/bin/bash
# repeat: repeat a command

repeat() {
	local sep=false
	# Check for separator flag
	if [ "$1" = "--sep" ] || [ "$1" = "-s" ]; then
		sep=true
		shift
	fi
	# Handle optional -- argument separator
	if [ "$1" = "--" ]; then
		shift
	fi
	set -a
	reps=$1 ; shift
	rep=0
	# Main loop
	while [ $rep -lt $reps ]; do
	# run the command to be repeated
		"$@"
		# Print separator if flag is set
		if $sep; then
			echo
		fi
		rep=$(($rep + 1))
	done
}

if [ "$0" = "$BASH_SOURCE" ]; then
	repeat "$@"
fi
