times - macOS


Overview

The times command in macOS allows users to execute a specified command multiple times, with a specified delay between each execution. This can be useful for automating repetitive tasks or creating custom loops in the command line.

Syntax

times [-h] [-l] [-n COUNT] [-t TIME] [-e ERRORS] COMMAND

Options/Flags

  • -h, –help: Displays the help message.
  • -l, –loop: Repeats the command indefinitely (until interrupted).
  • -n COUNT, –count=COUNT: Specifies the number of times to execute the command. Default is 1.
  • -t TIME, –time=TIME: Sets the delay between each execution in seconds. Default is 0 (no delay).
  • -e ERRORS, –errors=ERRORS: Limits the number of errors tolerated before stopping. Default is 0 (unlimited errors).

Examples

Simple Loop:

times echo "Hello world!"

Limited Execution:

times -n 5 echo "Executing five times"

Delayed Execution:

times -t 2 ls -lh

Common Issues

  • Command not Found: Ensure that the command being executed exists in the system path.
  • Syntax Error: Verify that the command syntax is correct and all required arguments are provided.
  • Insufficient Permissions: The user may not have sufficient permissions to execute the specified command.

Integration

Using with Other Commands:

ls -lh | times -n 3 grep "file.txt"

Using in Scripts:

#!/bin/sh

count=5
time=1

for i in $(seq 1 $count); do
    times -t $time echo "Iteration $i"
done
  • repeat: Executes a command multiple times in a loop.
  • while: Executes a command as long as a condition is met.
  • sleep: Suspends execution for a specified period.