I created a repeat function that is ridiculously useful for testing programs that produce variable output.
the function
add this to your bash shell either by pasting as a block into your terminal and then typing the function name to run, or add it to the appropriate dotfile to make it permanently available.
# this function will run a command multiple times, eg repeat 10 gibberish uuid or repeat 5 fortune
repeat() {
if [[ -z "$1" || ! "$1" =~ ^[0-9]+$ ]]; then
echo "Usage: repeat <count> <command> [args...]" >&2
return 1
fi
local count="$1"
shift
for i in $(seq "$count"); do
"$@"
done
}
usage
to use, invoke with repeat and a numerical argument. i.e.
┌──[ grumble@shinobi ]:~
└─$ repeat 5 gibberish numbers
84451182630363300096
77401767437641923164
42951978581655852424
06803068919499777616
43251993640144857502
or
┌──[ grumble@shinobi ]:~
└─$ repeat 2 uuidgen
547f5194-0b61-4f7b-9274-cab9ad1fd8ec
0e535674-0857-4172-a366-63b2ef1fa39d
This is useful for programs that produce variable or randomized output where running multiple times would make sense. Kind of a sibling to meta-execution and orchestration tools like watch, inotify, fswatch, xargs and more.
notes
The code above works on modern bash and zsh shells. For a POSIX-compliant, .sh version you can use this older but still functionally equivalent version.
repeat() {
if [ -z "$1" ]; then
echo "Usage: repeat <count> <command> [args...]" >&2
return 1
fi
case $1 in
''|*[!0-9]*) echo "Count must be a positive integer." >&2; return 1;;
esac
count=$1
shift
i=1
while [ "$i" -le "$count" ]; do
"$@"
i=`expr $i + 1`
done
}
I prefer the cleaner modern bash idioms, but there you go!